1
這是我用最新的流利驗證庫設置的代碼。fluentvalidation不顯示錯誤或更新Modelstate
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();
VIEW:
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
也是不正確的,它總是如此。
請問您的表單元素(HTML源代碼)顯示了不顯眼的驗證特性正在呈現(即數據val要求)?你可以顯示錶單元素的渲染輸出嗎? –