这是我使用最新的流利验证库设置的代码。
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; } }
全球.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
也不正确,它总是正确的 。