How do Data Annotations work?

后端 未结 4 1276
星月不相逢
星月不相逢 2020-12-08 15:08

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

4条回答
  •  甜味超标
    2020-12-08 15:49

    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

    Manually Validating Values

    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.

提交回复
热议问题