具有前缀的控件的Asp.Net MVC2 Clientside验证问题

问题是:当我在页面上放置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&lt; PhoneViewModel&gt;:
<%= 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个规则(必需,数量) alt text http://www.freeimagehosting.net/uploads/37fbe720bf.png 问题与Asp.Net MVC2客户端验证和重复ID问题有些重复 但建议手动生成ID并没有帮助。     
已邀请:
为文本框和验证设置相同前缀的正确方法:
<% using (Html.BeginHtmlFieldPrefixScope(Model.Prefix)) { %>
   <%= Html.TextBoxFor(m => m.Address.PostCode) %>
   <%= Html.ValidationMessageFor(m => m.Address.PostCode) %>
<% } %>
哪里
public static class HtmlPrefixScopeExtensions
{
    public static IDisposable BeginHtmlFieldPrefixScope(this HtmlHelper html, string htmlFieldPrefix)
    {
        return new HtmlFieldPrefixScope(html.ViewData.TemplateInfo, htmlFieldPrefix);
    }

    private class HtmlFieldPrefixScope : IDisposable
    {
        private readonly TemplateInfo templateInfo;
        private readonly string previousHtmlFieldPrefix;

        public HtmlFieldPrefixScope(TemplateInfo templateInfo, string htmlFieldPrefix)
        {
            this.templateInfo = templateInfo;

            previousHtmlFieldPrefix = templateInfo.HtmlFieldPrefix;
            templateInfo.HtmlFieldPrefix = htmlFieldPrefix;
        }

        public void Dispose()
        {
            templateInfo.HtmlFieldPrefix = previousHtmlFieldPrefix;
        }
    }
}
(偶然发现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 Prefix     

要回复问题请先登录注册