简体   繁体   English

如何在视图上显示fluentvalidation错误消息

[英]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 . 我用truefalse尝试了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.

相关问题 如何使用 FluentValidation 覆盖 int 的默认错误消息? - How to override the default error message for int using FluentValidation? 如何使用FluentValidation获得单选按钮的必需错误消息 - How to get a required error message for radio buttons using FluentValidation 在MVC3应用中使用FluentValidation的错误消息 - Error Message Using FluentValidation in MVC3 app FluentValidation - 如何在运行时自定义验证消息 - FluentValidation - How to customize the validation message in runtime FluentValidation 如何以多种语言返回消息 - FluentValidation how to return message in several languages FluentValidation:使用来自其他属性的值自定义错误消息 - FluentValidation: Customizing the error message with values from other properties 使用我的自定义 FluentValidation 错误消息而不是默认错误消息 - Use my custom FluentValidation error message instead of the default one ASP.Net MVC错误验证-将自定义视图模型传递给视图时如何显示验证消息 - ASP.Net MVC Error Validation - How to display validation message when passing a custom view model to a view FluentValidation NotEmpty消息未显示 - FluentValidation NotEmpty message is not showing 嵌套属性的 FluentValidation 消息 - FluentValidation message for nested properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM