当使用View客户端验证IEnumerable的不工作? [英] When use IEnumerable in view client side validation is not working?
问题描述
我从型号通过列表来查看,所以我针对指定这样的
IEnumerable的。在这种情况下客户端验证器不点火
查看:
@model IEnumerable的< ShoppingCart.Models.ShoppingClass>
@ {
ViewBag.Title =显示;}
<脚本的src =@ Url.Content(〜/脚本/ jquery.validate.js)TYPE =文/ JavaScript的>< / SCRIPT>
<脚本的src =@ Url.Content(〜/脚本/ jquery.validate.unobtrusive.js)TYPE =文/ JavaScript的>< / SCRIPT>@ Html.ValidationSummary(真)
@using(Html.BeginForm())
{ <表>
&所述; TR>
< TD> @ Html.Label(名优产品)
< / TD>
< TD> @ Html.TextBox(名优产品)
< DIV>
@ Html.ValidationMessage(名优产品)< / DIV>
< / TD>
&所述; TD>
<输入类型=提交值=搜索NAME =搜索/>
< / TD>
< / TR>
< /表>}
你写的什么是错的。
您会得到强类型的视图但你不使用强类型的属性或验证消息。此外,你已经得到了形式之外的验证摘要..这是行不通的。
@using(Html.BeginForm())
@ Html.ValidationSummary(真)
然后,你需要做这样的事情:
<表>
@foreach(ShoppingClass shoppingClass在模型){
&所述; TR>
< TD> @ Html.LabelFor(X => x.BrandName)
< / TD>
< TD> @ Html.TextBoxFor(X => x.BrandName)
< DIV>
@ Html.ValidationMessageFor(X => x.BrandName)LT; / DIV>
< / TD>
< / TR>
}
< /表>
<输入类型=提交值=搜索NAME =搜索/>
我不知道为什么你必须为每个去相同的控制器和操作项的搜索按钮..但我会离开,你要弄清楚。
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>
}
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.
这篇关于当使用View客户端验证IEnumerable的不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!