fluentvalidation不显示错误或更新Modelstate

时间:2012-11-07 10:23:01

标签: c# asp.net-mvc-4 fluentvalidation

这是我使用最新的流畅验证库设置的代码。

public class ReleaseViewModelValidator : AbstractValidator<ReleaseViewModel> 
{ 
   public ReleaseViewModelValidator() {
     RuleFor(r => r.Name).NotEmpty().Length(1, 30).WithMessage("Name must not be longer than 30 chars."); 
  } 
}

[FluentValidation.Attributes.Validator(typeof(ReleaseViewModel))]
 public class ReleaseViewModel { public int ReleaseId { get; set; }

[Remote("ReleaseExists", "Release", ErrorMessage = "This name already exists.")]
public string Name { get; set; } 
public DateTime CreatedAt { get; set; } }

GlOBAL.ASAX:

 FluentValidationModelValidatorProvider.Configure();

查看:

 model ILMD.Web.Models.ReleaseViewModel @* Remote Validation*@ <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
 @using (Html.BeginForm("Create", "Release")) 
 {
 <p class="editor-label">@Html.LabelFor(model => model.Name)</p> 
 <p class="editor-field">@Html.EditorFor(model => model.Name)</p> 
 <p class="editor-field">@Html.ValidationMessageFor(model => model.Name)</p> 
 }

ReleaseViewModelValidator的所有单元测试都显示绿灯。细

但不太酷的是输入一些实时数据,如31个字符或输入任何内容,我没有看到任何客户端错误消息。

我还需要在局部视图中包含一些内容吗? ModelState也不正确,总是如此。

1 个答案:

答案 0 :(得分:1)

试试这个

[FluentValidation.Attributes.Validator(typeof(ReleaseViewModelValidator))]

因为在FluentValidation属性验证程序中,您指定了实际验证程序(ReleaseViewModelValidator)而不是ReleaseViewModel