如何数据注释工作? [英] How do Data Annotations work?

查看:100
本文介绍了如何数据注释工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用数据注释在我的ASP.NET MVC 3项目来验证模型。这些是非常方便的,但目前他们的魔法给我。我读了数据的注释做的的抛出异常。怎么那么MVC知道验证错误添加到模型状态字典?如何失败的设置,因为模型验证泡到MVC模型的属性,如果没有异常被抛出?我一直认为异常被抛出的每一个属性失败和MVC模型绑定捕捉到的异常,并将其添加到模型状态字典的时间。

I use Data Annotations in my ASP.NET MVC 3 project to validate the model. These are extremely convenient but currently they are magic to me. I read that data annotations do not throw exceptions. How then does MVC know to add validation errors to the model state dictionary? How does the failure to set a property on the model because of model validation bubble up to MVC if no exception is thrown? I always assumed that exceptions were thrown every time a property failed and that MVC model binding caught the exception and added it to the model state dictionary.

要测试这个我创建了一个控制台应用程序,添加了验证注解的样本类是:

To test this I created a console application and added a sample class with a validation annotation to it:

public class MyObject
{
    [StringLength(10, MinimumLength=3)]
    public string Name { get; set; }
}

然后我实例化对象,并试图赋值给小于3分配就好了属性的名称属性,尽管说的不到3字符串长度是不允许的注释。

I then instantiated the object and tried to assign values to the Name property that were less than 3. The property assigned just fine, despite the annotation that says string length of less than 3 is not allowed.

    static void Main(string[] args)
    {
        MyObject mine = new MyObject();
        mine.Name = "hi";
        Console.WriteLine(mine.Name);
        Console.ReadLine();
    }

这个小程序写出嗨到控制台。为什么?我期待它试图建立 mine.Name 来嗨的时候上火。

This little program writes out "hi" to the console. Why? I was expecting it to get angry when trying to set mine.Name to "hi".

我是什么失踪?

先谢谢了。

推荐答案

您永远不会调用任何验证的属性。验证不会对自己的神奇发生。从<一个href=\"http://msdn.microsoft.com/en-us/library/dd901590%28v=vs.95%29.aspx\">http://msdn.microsoft.com/en-us/library/dd901590%28v=vs.95%29.aspx

You never call anything to validate the properties. The validation doesn't happen magically on its own. from http://msdn.microsoft.com/en-us/library/dd901590%28v=vs.95%29.aspx

手动验证值

当你不使用DataGrid
  控制,以提供对接口
  编辑数据,验证
  属性不是自动
  应用。但是,您可以手动
  通过应用验证测试
  验证类。您可以调用
   ValidateProperty 的设置方法
  一个属性访问器来检查
  对确认的价值
  属性的属性。你必须
  还同时设置了 ValidatesOnExceptions
  和 NotifyOnValidationError 属性
  为真时,数据绑定接收
  从验证验证异常
  属性。对于手动为例
  应用验证,看数据
  下面结合实例。

When you do not use the DataGrid control to provide the interface for editing data, the validation attributes are not automatically applied. However, you can manually apply the validation test by using the Validator class. You can call the ValidateProperty method on the set accessor of a property to check the value against the validation attributes for the property. You must also set bothValidatesOnExceptions and NotifyOnValidationError properties to true when data binding to receive validation exceptions from validation attributes. For an example of manually applying validation, see the Data Binding Example below.

这篇关于如何数据注释工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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