Validation MVC3中有没有一种方法可以为类中的组定义数据验证注释?
我是MVC的新手,最近我正在进行数据验证,我想知道,除了为每个参数提供验证注释外,是否有一种方法可以为类中的一组参数定义验证规则?例如,一个类是这样的:Validation MVC3中有没有一种方法可以为类中的组定义数据验证注释?,validation,asp.net-mvc-3,model,Validation,Asp.net Mvc 3,Model,我是MVC的新手,最近我正在进行数据验证,我想知道,除了为每个参数提供验证注释外,是否有一种方法可以为类中的一组参数定义验证规则?例如,一个类是这样的: namespace MvcApplication1.Models { public class Product { public int Id { get; set; } [Required] [StringLength(10)] public string P
namespace MvcApplication1.Models
{
public class Product
{
public int Id { get; set; }
[Required]
[StringLength(10)]
public string Param1 { get; set; }
[Required]
public string Param2 { get; set; }
[DisplayName("Param3")]
[Required]
public string Param3 { get; set; }
}
}
是否有一种方法可以定义一个规则,例如,对于Param1、Param2和Param3,至少需要两个规则?对于这样更高级的验证,我建议使用FluentValidation 您可以创建自己的自定义验证器:
对于像这样更高级的验证,我建议使用FluentValidation 您可以创建自己的自定义验证器:
非常容易使用。试试这个。 这就是如何构建自己的自定义验证。
非常容易使用。试试这个。 这就是如何构建自己的自定义验证。
Darin的回答定义您自己的自定义验证哇,这正是我想要的。非常感谢。Darin的答案是定义您自己的自定义验证哇,这正是我想要的。非常感谢。
DataType
Specify the datatype of a property
DisplayName
specify the display name for a property.
DisplayFormat
specify the display format for a property like different format for Date proerty.
Required
Specify a property as required.
ReqularExpression
validate the value of a property by specified regular expression pattern.
Range
validate the value of a property with in a specified range of values.
StringLength
specify min and max length for a string property.
MaxLength
specify max length for a string property.
Bind
specify fields to include or exclude when adding parameter or form values to model properties.
ScaffoldColumn
specify fields for hiding from editor forms.