ModelError和ValidationResult之间有什么区别? [英] What is the difference between ModelError and ValidationResult?

查看:638
本文介绍了ModelError和ValidationResult之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP.NET MVC存在一个的ModelState 类,它包含ModelErrorCollection。和ModelError代表模型绑定过程中发生错误。

In ASP.NET MVC there exists a ModelState class that contains ModelErrorCollection. And a ModelError represents an error the occurs during model binding.

我知道的为ValidationResult 从ValidationAttribute.IsValid方法返回,并验证指定的值相对于所述当前验证属性。据我所知,我们可以从validationAttribute继承,为了编写自定义模型验证属性来覆盖的IsValid()。

I know that ValidationResult is returned from ValidationAttribute.IsValid Method, and validates the specified value with respect to the current validation attribute. I understand that we can inherit from validationAttribute and override IsValid() in order to write a custom model validation attribute.

使用示例的ModelState http://www.asp.net/mvc/tutorials/older-versions/models-(data)/performing-simple-validation-cs

使用示例为ValidationResult http://www.codeproject.com/Articles/260177/Custom-Validation-Attribute-in-ASP-NET-MVC

如何做这些两人携手?或不能协同工作?什么交集?这个问题是想了解ASP.NET MVC的验证方法更好。

How do these two work together? Or not work together? What is the intersection? This question is trying to understand ASP.NET MVC validation approaches better.

为什么我感兴趣,为什么不使用数据注解?答案是,我想更好地了解在ASP.NET MVC的动态字段的验证。在我的产品情况下,我不会有预先确定属性的模型validationAttributes重视。

Why am I interested and why not just use data annotations? The answer is, I want to better understand validation for dynamic fields in ASP.NET MVC. In my product context, I will not have a model of pre-determined properties to attach validationAttributes to.

推荐答案

我是新来ASP.NET MVC。从我你提到的2的理解。

I am new to ASP.NET MVC. From my understanding of the 2 you mentioned.

为ValidationResult 给我们的能力来定制我们的错误信息被链接到一个属性方法简单地实现 IValidatableObject ,并得到每个模型定义验证方法。
所以从我的角度来看,为ValidationResult更像是用于配置的条件和模型验证结果的功能。

ValidationResult give us the ability to custom our error message linked to a property method by simply implement the IValidatableObject and get each model to define the Validate method. So from my point of view, ValidationResult is more like a feature used for configure the conditions and results of model validation.

ModelState中给我们做的可能性我们从为ValidationResult自定义错误的ModelState中的一部分,通过做 ModelState.AddModelError(memberName,validationResult.ErrorMessage)

ModelState give us the possibility to make our custom error from ValidationResult as part of the ModelState by doing ModelState.AddModelError(memberName, validationResult.ErrorMessage)

并与为ValidationResult和ModelState中,我们仍然可以使用如果(ModelState.IsValid)在我们的控制器,而内部行为已经包含了我们的自定义错误消息和确认条件的组合。

And With the combination of ValidationResult and ModelState, we can still using if (ModelState.IsValid) in our controller while the internal behavior already contains our custom error message and validation conditions.

帕特里克曾发布关于MVC中的验证的详细的文章,你可以参考的这里

Patrick had posted an detailed article regarding the validation in MVC, you can refer to here

这篇关于ModelError和ValidationResult之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆