Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

asp.net mvc 3 - When use IEnumerable in view client side validation is not working?

I am passing List from Model to view , so I specified in view like this IEnumerable. In this situation Client side validation is not firing

View :

@model  IEnumerable<ShoppingCart.Models.ShoppingClass>
@{
    ViewBag.Title = "Display";

}
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>

@Html.ValidationSummary(true)
@using (Html.BeginForm())
{

    <table>
        <tr>
            <td>@Html.Label("BrandName")
            </td>
            <td>@Html.TextBox("BrandName")
                <div>
                    @Html.ValidationMessage("BrandName")</div>
            </td>
            <td>
                <input type="submit" value="Search" name="Search" />
            </td>
        </tr>
    </table>

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What you've written is wrong.

You're got the view strongly typed but then you're not using the strongly typed properties or validation messages. Also, you've got the validation summary outside the form.. which won't work.

@using (Html.BeginForm())
@Html.ValidationSummary(true)

Then, you'll need to do something like this:

<table>
    @foreach (ShoppingClass shoppingClass in Model) {
    <tr>
        <td>@Html.LabelFor(x => x.BrandName)
        </td>
        <td>@Html.TextBoxFor(x => x.BrandName)
            <div>
                @Html.ValidationMessageFor(x => x.BrandName)</div>
        </td>
    </tr>
    }
</table>
<input type="submit" value="Search" name="Search" />

I'm not sure why you have a Search button for every item that goes to the same controller and action.. but I'll leave that for you to figure out.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...