[英]How to display fluentvalidation error message on view
I've a fluent validator as below 我有一个流利的验证器,如下所示
[Validator(typeof(testvalidator))]
public class test
{
.....
}
public class testvalidator: AbstractValidator<test>
{
public testvalidator()
{
Custom(testvalidationmethod);
}
public ValidationFailure testvalidationmethod(test t)
{
return ValidationFailure("","error occurred in test validation method ");
}
}
Usage 用法
testvalidator tv = new testvalidator();
var result =tv.(test);
I can see the error message in the result.Errors
. 我可以在
result.Errors
看到错误消息。 But I want to display the error message on the view. 但是我想在视图上显示错误消息。 I tried
Html.ValidationSummary()
with true
and false
. 我用
true
和false
尝试了Html.ValidationSummary()
。 But didn't work. 但是没有用。 What need to be done to display on the view?
需要做什么来显示在视图上? I was reading tutorial .
我在看教程 。 But it shows how to display on the
console
, which is hardly required in the real application. 但是它显示了如何在
console
上显示,这在实际应用程序中几乎不需要。 How do I make it work? 我该如何运作?
There is a method named AddToModelState
, which needs to be called to add errors to ModelState
. 有一个名为
AddToModelState
的方法,需要调用该方法才能向ModelState
添加错误。 After that errors started showing up in the view. 之后,错误开始显示在视图中。
Usage 用法
public ActionResult DoSomething() {
var customer = new Customer();
var validator = new CustomerValidator();
var results = validator.Validate(customer);
results.AddToModelState(ModelState, null);
return View();
}
Source of above fluentvalidation on github . 上面fluentvalidation在github上的来源。 Look at Manual Validations.
查看手动验证。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.