ASP.Net MVC 2 RC:如何使用客户端验证和列表数据注释?
我的问题:
我无法让数据注释客户端验证与我的 viewdata 类中的列表一起使用。
瘦身:
在我的视图数据类中,我有一个列表。
public class FriendsViewData
{
public List<Person> people { get; set; }
}
我使用数据注释拥有了 Person 类的所有属性。
public class Person
{
[Required(ErrorMessage="First Name is required")]
public string FirstName { get; set; }
}
在视图中,我像这样循环列表: ...
<script src="/Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>
<% Html.EnableClientValidation(); %>
<% using (Html.BeginForm()) {%>
<%
for(int i=0; i < Model.people.Count; i++)
{%>
<div>
<%= Html.TextBox(string.Format("people[{0}].FirstName",i), Model.people[i].FirstName)%>
<%= Html.ValidationMessage(string.Format("people[{0}].FirstName", i))%>
</div>
<% } %>
<input type="submit" value="Submit" />
<% } %>
生成的 HTML:
<div>
<input id="people_0__FirstName" name="people[0].FirstName" type="text" value="Name0" />
<span class="field-validation-valid" id="form0_people_0__FirstName_validationMessage"></span>
</div>
<div>
<input id="people_1__FirstName" name="people[1].FirstName" type="text" value="Name1" />
<span class="field-validation-valid" id="form0_people_1__FirstName_validationMessage"></span>
</div>
<div>
<input id="people_2__FirstName" name="people[2].FirstName" type="text" value="Name2" />
<span class="field-validation-valid" id="form0_people_2__FirstName_validationMessage"></span>
</div>
结果:
根本不起作用。
我尝试过的其他事情:
尝试在视图中使用这些 HTML Helper 方法:
<div>
<%= Html.TextBoxFor(model => model.people[i].FirstName) %>
<%= Html.ValidationMessageFor(model => model.people[i].FirstName) %>
</div>
生成的输出:
<div>
<input id="FirstName" name="FirstName" type="text" value="Name0" />
<span class="field-validation-valid" id="form0_FirstName_validationMessage"></span>
</div>
<div>
<input id="FirstName" name="FirstName" type="text" value="Name1" />
<span class="field-validation-valid" id="form0_FirstName_validationMessage"></span>
</div>
<div>
<input id="FirstName" name="FirstName" type="text" value="Name2" />
<span class="field-validation-valid" id="form0_FirstName_validationMessage"></span>
</div>
结果:
有趣的是,当我触发在第一个文本框上进行验证,它会同时针对所有文本框触发。其余文本框根本不会触发验证。
请注意,文本框及其相应错误文本范围元素的所有生成 ID 和名称都是相同的。
有谁知道如何在视图数据中使用客户端验证和列表,支持模型绑定?
谢谢!
My problem:
I can't get Data Annotations Client Validation to work with a List in my viewdata class.
The skinny:
In my view data class I have a List.
public class FriendsViewData
{
public List<Person> people { get; set; }
}
I have all the properties of the class Person as required using Data Annotations.
public class Person
{
[Required(ErrorMessage="First Name is required")]
public string FirstName { get; set; }
}
In the View I loop the List like this:
...
<script src="/Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>
<% Html.EnableClientValidation(); %>
<% using (Html.BeginForm()) {%>
<%
for(int i=0; i < Model.people.Count; i++)
{%>
<div>
<%= Html.TextBox(string.Format("people[{0}].FirstName",i), Model.people[i].FirstName)%>
<%= Html.ValidationMessage(string.Format("people[{0}].FirstName", i))%>
</div>
<% } %>
<input type="submit" value="Submit" />
<% } %>
Generated HTML:
<div>
<input id="people_0__FirstName" name="people[0].FirstName" type="text" value="Name0" />
<span class="field-validation-valid" id="form0_people_0__FirstName_validationMessage"></span>
</div>
<div>
<input id="people_1__FirstName" name="people[1].FirstName" type="text" value="Name1" />
<span class="field-validation-valid" id="form0_people_1__FirstName_validationMessage"></span>
</div>
<div>
<input id="people_2__FirstName" name="people[2].FirstName" type="text" value="Name2" />
<span class="field-validation-valid" id="form0_people_2__FirstName_validationMessage"></span>
</div>
Result:
Didn't work at all.
Other things I've tried:
Tried using these HTML Helper methods in the view instead:
<div>
<%= Html.TextBoxFor(model => model.people[i].FirstName) %>
<%= Html.ValidationMessageFor(model => model.people[i].FirstName) %>
</div>
Generated output:
<div>
<input id="FirstName" name="FirstName" type="text" value="Name0" />
<span class="field-validation-valid" id="form0_FirstName_validationMessage"></span>
</div>
<div>
<input id="FirstName" name="FirstName" type="text" value="Name1" />
<span class="field-validation-valid" id="form0_FirstName_validationMessage"></span>
</div>
<div>
<input id="FirstName" name="FirstName" type="text" value="Name2" />
<span class="field-validation-valid" id="form0_FirstName_validationMessage"></span>
</div>
Result:
Interestingly, when I trigger a validation on the first textbox, it fires simultaneously for all of the textboxes. The rest of the textboxes do not trigger validation at all.
Note all generated id and names for the textboxes and their corresponding error text span element, are all identical.
Does any know how to use Client Validation with a List in the view data, supporting ModelBinding?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ASP.Net 团队的 Brad Wilson 在 ASP.Net 论坛中回答了我同样的问题。
这是正确的方法,但它现在已经损坏,并将在下一个版本中修复。
http://forums.asp.net/t/1518900.aspx
Brad Wilson from the ASP.Net team answered my same question in the ASP.Net forums.
This is the correct way to do it, but it's broken right now, and will be fixed in the next drop.
http://forums.asp.net/t/1518900.aspx