【问题标题】:Validation message contain '{PropertyName}' instead of name of property验证消息包含“{PropertyName}”而不是属性名称
【发布时间】:2011-01-31 10:32:53
【问题描述】:

我使用流畅的验证和客户端不显眼的验证。

 <fieldset class="edit-root-form">
    <p>
        @Html.LabelFor(model => model.Login, UserRes.Login)
        @Html.TextBoxFor(model => model.Login)
        @Html.ValidationMessageFor(model => model.Login)
    </p>         
</fieldset>

流利的验证规则:

 this.RuleFor(x => x.Login).NotNull().EmailAddress()

我收到如下错误消息:'{PropertyName}' 不能为空。

生成的html:

<input data-val="true" data-val-regex="&amp;#39;{PropertyName}&amp;#39; is not a valid
email address."  data-val-required="&amp;#39;{PropertyName}&amp;#39; must not be
 empty." id="Login" name="Login" type="text" value="" class="input-validation-error">

为什么 MVC 不替换 PropertyName 真实的字段名?

【问题讨论】:

    标签: c# asp.net-mvc-3 unobtrusive-validation fluentvalidation


    【解决方案1】:

    对我来说很好用。我正在使用最新的 FluentValidation 版本 (2.0.0.0) 和 ASP.NET MVC 3 RTM。

    模型和验证器:

    [Validator(typeof(MyViewModelValidator))]
    public class MyViewModel
    {
        public string Login { get; set; }
    }
    
    public class MyViewModelValidator : AbstractValidator<MyViewModel>
    {
        public MyViewModelValidator()
        {
            RuleFor(x => x.Login).NotNull().EmailAddress();
        }
    }
    

    控制器:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View(new MyViewModel());
        }
    
        [HttpPost]
        public ActionResult Index(MyViewModel model)
        {
            return View(model);
        }
    }
    

    查看:

    @model AppName.Models.MyViewModel
    <script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
    @using (Html.BeginForm())
    {
        @Html.LabelFor(model => model.Login, "Login")
        @Html.TextBoxFor(model => model.Login)
        @Html.ValidationMessageFor(model => model.Login)
        <input type="submit" value="OK" />
    }
    

    Application_Start in Global.asax:

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
    
        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
        ModelValidatorProviders.Providers.Add(new FluentValidationModelValidatorProvider(new AttributedValidatorFactory()));
    }
    

    两种情况:

    1. 将文本框留空:显示'Login' must not be empty. 验证错误消息。
    2. 键入无效电子邮件:显示'Login' is not a valid email address. 验证错误消息。

    最后是为文本框生成的 HTML:

    <input data-val="true" data-val-regex="&amp;#39;Login&amp;#39; is not a valid email address." data-val-regex-pattern="^(?:[\w\!\#\$\%\&amp;\&#39;\*\+\-\/\=\?\^\`\{\|\}\~]+\.)*[\w\!\#\$\%\&amp;\&#39;\*\+\-\/\=\?\^\`\{\|\}\~]+@(?:(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!\.)){0,61}[a-zA-Z0-9]?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!$)){0,61}[a-zA-Z0-9]?)|(?:\[(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\]))$" data-val-required="&amp;#39;Login&amp;#39; must not be empty." id="Login" name="Login" type="text" value="" />
    

    【讨论】:

    • 我已经使用您的示例创建了项目。而且我仍然看到 {PropertyName} 而不是真实的字段名称。我使用与您相同的 FluentValidation 版本 (2.0.0.0) 和 ASP.NET MVC 3 版本 3.0.11209.0
    • @Ivan,看来您没有使用 RTM 版本。我的System.Web.Mvc 程序集的版本为3.0.20105.0,即RTM。
    • @Ivan,是的,但是您是否卸载了任何以前的版本?还要确保您的应用程序指向正确的程序集。
    • 我将 MVC 更新到 3.0.20105。但是错误钢出现了。你能看看我的例子吗:rapidshare.com/files/445598384/TestValidation.zip
    • @Ivan,确实如此。看起来问题来自 FluentValidation 的版本。我的文件是 80384 字节,而你的是 79360 字节,所以版本不一样。当我用我的替换 FluentValidation.dll 和 FluentValidation.Mvc.dll 时,它工作正常。我直接从网站下载了我的:2011 年 1 月 13 日星期四上午 9:00
    猜你喜欢
    • 1970-01-01
    • 2021-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多