验证消息包含“{PropertyName}”而不是属性名称

Iva*_*tin 5 c# fluentvalidation unobtrusive-validation asp.net-mvc-3

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

 <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>
Run Code Online (Sandbox Code Playgroud)

流畅的验证规则:

 this.RuleFor(x => x.Login).NotNull().EmailAddress()
Run Code Online (Sandbox Code Playgroud)

我收到了这样的错误消息:“ {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">
Run Code Online (Sandbox Code Playgroud)

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

Dar*_*rov 3

对我来说效果很好。我正在使用最新的 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

控制器:

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

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        return View(model);
    }
}
Run Code Online (Sandbox Code Playgroud)

看法:

@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" />
}
Run Code Online (Sandbox Code Playgroud)

Application_StartGlobal.asax

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);
    ModelValidatorProviders.Providers.Add(new FluentValidationModelValidatorProvider(new AttributedValidatorFactory()));
}
Run Code Online (Sandbox Code Playgroud)

两种情况:

  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="" />
Run Code Online (Sandbox Code Playgroud)

  • @Ivan,确实是的。看来问题出在 FluentValidation 的版本上。我的文件是80384字节,而你的是79360字节,所以不是同一版本。当我用我的替换 FluentValidation.dll 和 FluentValidation.Mvc.dll 时,它工作正常。我直接从网站下载了我的:2011 年 1 月 13 日星期四上午 9:00 (2认同)