我已经制作了一个简单的模型,其中包含FluentValidation,但它似乎不起作用。如何将FluentValidation集成到MVC4中
- 我的数据库是空的名字更新和传递
TryUpdateModel()
- 当我提交表单
我试图在Application_Start()
添加FluentValidationModelValidatorProvider.Configure();
我不明白客户端验证错误,但它表明它即使我使用类添加也找不到FluentValidationModelValidatorProvider
。我也试着在我的模特课上添加[Validator(typeof(Category))]
,但没有做任何事情。 This我们一直在寻找信息的资源。
型号
public class Category
{
public int ID { get; set; }
public string Name { get; set; }
virtual public ICollection<Image> Images { get; set; }
}
public class CategoryValidator : AbstractValidator<Category>
{
public CategoryValidator()
{
RuleFor(x => x.Name).NotEmpty().WithMessage("Category name is required.");
}
}
控制器
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Category c)
{
var category = _db.Categories.Where(x => x.ID == c.ID).SingleOrDefault();
if (category == null) return HttpNotFound();
// Update model and return to category list
if (TryUpdateModel(category)) // it passes with empty name and saves changes
{
_db.SaveChanges();
return RedirectToAction("index", "category");
}
// Something is wrong, return view back
return View(c);
}
什么是使用不起作用的声明? – jrummell
@ jrummell'使用FluentValidation;' – sed
尝试'使用FluentValidation.Mvc;' – jrummell