ASP.NETMVCプロジェクトで流暢な検証を使用しようとしています。ビューモデルを検証しようとしています。
これは私のビューモデルです、
[Validator(typeof(ProductCreateValidator))]
public class ProductCreate
{
public string ProductCategory { get; set; }
public string ProductName { get; set; }
....
}
これは私のバリデータークラスです、
public class ProductCreateValidator : AbstractValidator<ProductCreate>
{
public ProductCreateValidator()
{
RuleFor(product => product.ProductCategory).NotNull();
RuleFor(product => product.ProductName).NotNull();
}
}
そして、私のコントローラーでは、ModelStateが有効かどうかをチェックしています。
[HttpPost]
public ActionResult Create(ProductCreate model)
{
/* This is a method in viewmodel that fills dropdownlists from db */
model.FillDropDownLists();
/* Here this is always valid */
if (ModelState.IsValid)
{
SaveProduct(model);
return RedirectToAction("Index");
}
// If we got this far, something failed, redisplay form
return View(model);
}
これは私が持っているものです。私の問題は、ビューモデルが完全に空になったときにModelState.IsValid
返されることです。true
モデルエラーをModalStateに追加できるように、Fluent検証を手動で構成する必要がありますか?