System.ComponentModel.DataAnnotations 命名空间和RequiredAttribute 类

System.ComponentModel.DataAnnotations 命名空间提供定义 ASP.NET MVC 和 ASP.NET 数据控件的类的特性。

RequiredAttribute

指定需要数据字段值。

https://msdn.microsoft.com/zh-cn/library/system.componentmodel.dataannotations.aspx

RequiredAttribute 类

 
 
 

指定需要数据字段值。

命名空间:   System.ComponentModel.DataAnnotations
程序集:  System.ComponentModel.DataAnnotations(System.ComponentModel.DataAnnotations.dll 中)


[AttributeUsageAttribute(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, 
    AllowMultiple = false)]
public class RequiredAttribute : ValidationAttribute
 名称说明
System_CAPS_pubmethod RequiredAttribute()

初始化 RequiredAttribute 类的新实例。

属性
 
 
 名称说明
System_CAPS_pubproperty AllowEmptyStrings

获取或设置一个值,该值指示是否允许空字符串。

System_CAPS_pubproperty ErrorMessage

获取或设置一条在验证失败的情况下与验证控件关联的错误消息。(从ValidationAttribute 继承。)

System_CAPS_pubproperty ErrorMessageResourceName

获取或设置错误消息资源的名称,在验证失败的情况下,要使用该名称来查找 ErrorMessageResourceType 属性值。(从 ValidationAttribute 继承。)

System_CAPS_pubproperty ErrorMessageResourceType

获取或设置在验证失败的情况下用于查找错误消息的资源类型。(从ValidationAttribute 继承。)

System_CAPS_protproperty ErrorMessageString

获取本地化的验证错误消息。(从 ValidationAttribute 继承。)

System_CAPS_pubproperty RequiresValidationContext

获取指示特性是否要求验证上下文的值。(从 ValidationAttribute 继承。)

System_CAPS_pubproperty TypeId

当在派生类中实现时,获取该 Attribute 的唯一标识符。(从 Attribute 继承。)

 
using System;
using System.Web.DynamicData;
using System.ComponentModel.DataAnnotations;
using System.Globalization;

[MetadataType(typeof(CustomerMetaData))]
public partial class Customer
{


}

public class CustomerMetaData
{
    // Require that the Title is not null.
    // Use custom validation error.
    [Required(ErrorMessage = "Title is required.")]
    public object Title;

    // Require that the MiddleName is not null.
    // Use standard validation error.
    [Required()]
    public object MiddleName;

}
原文地址:https://www.cnblogs.com/CandiceW/p/4938673.html