带有前缀的控件的 Asp.Net MVC2 客户端验证问题
问题是:当我在页面上放置 2 个相同类型的控件时,我需要指定不同的绑定前缀。在这种情况下,表单之后生成的验证规则不正确。那么如何获得该案例的客户端验证工作呢?:
页面包含:
<%
Html.RenderPartial(ViewLocations.Shared.PhoneEditPartial, new PhoneViewModel { Phone = person.PhonePhone, Prefix = "PhonePhone" });
Html.RenderPartial(ViewLocations.Shared.PhoneEditPartial, new PhoneViewModel { Phone = person.FaxPhone, Prefix = "FaxPhone" });
%>
控件 ViewUserControl
<%= Html.TextBox(Model.GetPrefixed("CountryCode"), Model.Phone.CountryCode) %>
<%= Html.ValidationMessage("Phone.CountryCode", new { id = Model.GetPrefixed("CountryCode"), name = Model.GetPrefixed("CountryCode") })%>
其中 Model.GetPrefixed("CountryCode")
仅返回“FaxPhone.CountryCode”或“PhonePhone. CountryCode”取决于前缀
这里是表单后生成的验证规则。它们对于字段名称“Phone.CountryCode”是重复的。虽然所需的结果是每个 FieldNames“FaxPhone.CountryCode”、“PhonePhone.CountryCode”有 2 条规则(必填,数字) 替代文本 http://www.freeimagehosting.net/uploads/37fbe720bf.png
问题与 Asp.Net MVC2 客户端验证和重复 ID 有点重复问题 但手动生成 ids 的建议没有帮助。
The problem is: when I put 2 controls of the same type on a page I need to specify different prefixes for binding. In this case the validation rules generated right after the form are incorrect. So how to get client validation work for the case?:
the page contains:
<%
Html.RenderPartial(ViewLocations.Shared.PhoneEditPartial, new PhoneViewModel { Phone = person.PhonePhone, Prefix = "PhonePhone" });
Html.RenderPartial(ViewLocations.Shared.PhoneEditPartial, new PhoneViewModel { Phone = person.FaxPhone, Prefix = "FaxPhone" });
%>
the control ViewUserControl<PhoneViewModel>:
<%= Html.TextBox(Model.GetPrefixed("CountryCode"), Model.Phone.CountryCode) %>
<%= Html.ValidationMessage("Phone.CountryCode", new { id = Model.GetPrefixed("CountryCode"), name = Model.GetPrefixed("CountryCode") })%>
where Model.GetPrefixed("CountryCode")
just returns "FaxPhone.CountryCode" or "PhonePhone.CountryCode" depending on prefix
And here is the validation rules generated after the form. They are duplicated for the field name "Phone.CountryCode". While the desired result is 2 rules (required, number) for each of the FieldNames "FaxPhone.CountryCode", "PhonePhone.CountryCode"
alt text http://www.freeimagehosting.net/uploads/37fbe720bf.png
The question is somewhat duplicate of Asp.Net MVC2 Clientside Validation and duplicate ID's problem
but the advise to manually generate ids doesn't helps.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为文本框和验证设置相同前缀的正确方法:
where
(偶然在 Steve Sanderson 博客 http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list- aspnet-mvc-2-style/)
看起来 Html.EditorFor 方法也应该像这里建议的那样工作:ASP.NET MVC 2 - ViewModel 前缀
Correct way to set the same prefixes both for textbox and validation:
where
(by chance found the solution in the code on Steve Sanderson's blog http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/)
Also looks like Html.EditorFor approach should work as well as suggested here: ASP.NET MVC 2 - ViewModel Prefix