【问题标题】:Asp.Net MVC2 Clientside Validation problem with controls with prefixes带有前缀的控件的 Asp.Net MVC2 客户端验证问题
【发布时间】:2010-05-03 15:31:58
【问题描述】:

问题是:当我在一个页面上放置 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”重复。虽然所需的结果是每个字段名称“FaxPhone.CountryCode”、“PhonePhone.CountryCode”的 2 个规则(必需,数字) alt text http://www.freeimagehosting.net/uploads/37fbe720bf.png

这个问题与Asp.Net MVC2 Clientside Validation and duplicate ID's problem有些重复 但是手动生成 id 的建议没有帮助。

【问题讨论】:

    标签: validation asp.net-mvc-2 client viewusercontrol prefixes


    【解决方案1】:

    为文本框和验证设置相同前缀的正确方法:

    <% 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;
            }
        }
    }
    

    (偶然在史蒂夫·桑德森的博客http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/的代码中找到了解决方案)

    看起来 Html.EditorFor 方法应该像这里建议的那样工作:ASP.NET MVC 2 - ViewModel Prefix

    【讨论】:

    • 不错。这个答案非常有帮助。希望我能再投票几次。
    • 非常非常有帮助。谢谢。
    • 我知道这是一个旧答案,但我想知道你的方法是否比把它放在你的视图中更可取:ViewData.TemplateInfo.HtmlFieldPrefix = "myViewModel.MyCustomObjdect";
    • 是的,因为它以 DRY 方式执行相同的操作等等:它在 Dispose 方法中恢复以前的 HtmlFieldPrefix。
    • 我认为这应该是 MVC 核心功能的一部分
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-21
    • 2011-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多