问题是:当我在页面上放置 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") })%>
where Model.GetPrefixed("CountryCode")
仅返回“传真电话.国家/地区代码”或“电话电话.国家/地区代码”,具体取决于前缀
这是表单后生成的验证规则。它们对于字段名称“Phone.CountryCode”是重复的。虽然所需的结果是每个 FieldNames“FaxPhone.CountryCode”、“PhonePhone.CountryCode”有 2 条规则(必填,数字)替代文本 http://www.freeimagehosting.net/uploads/37fbe720bf.png http://www.freeimagehosting.net/uploads/37fbe720bf.png
这个问题有点重复Asp.Net MVC2客户端验证和重复ID的问题 https://stackoverflow.com/questions/2675606/asp-net-mvc2-clientside-validation-and-duplicate-ids-problem但手动生成 ids 的建议没有帮助。