RequiredAttribute 类 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
指定数据字段值是必需的。
public ref class RequiredAttribute : System::ComponentModel::DataAnnotations::ValidationAttribute[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property, AllowMultiple=false)]
public class RequiredAttribute : System.ComponentModel.DataAnnotations.ValidationAttribute[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Property, AllowMultiple=false)]
public class RequiredAttribute : System.ComponentModel.DataAnnotations.ValidationAttribute[<System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property, AllowMultiple=false)>]
type RequiredAttribute = class
    inherit ValidationAttribute[<System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Property, AllowMultiple=false)>]
type RequiredAttribute = class
    inherit ValidationAttributePublic Class RequiredAttribute
Inherits ValidationAttribute- 继承
- 属性
示例
以下示例使用 RequiredAttribute 属性替代允许数据字段为空的数据库架构规则。 该示例执行以下步骤:
- 实现元数据分部类和关联的元数据类。 
- 在关联的元数据类中,它应用 RequiredAttribute 属性,该属性指定以下要求: - 标题数据字段不能为空。 如果验证失败,示例中的代码将引发验证异常并显示错误消息。 错误消息是在将 属性应用于数据字段时指定的。 
- MiddleName 数据字段不能为空。 如果验证失败,示例中的代码将引发验证异常并显示错误消息。 
 
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;
}
Imports System.Web.DynamicData
Imports System.ComponentModel.DataAnnotations
Imports System.Globalization
<MetadataType(GetType(CustomerMetaData))> _
Partial Public Class Customer
   
End Class
Public Class CustomerMetaData
    ' Require that the Title is not null.
    ' Use custom validation error.
    <Required(ErrorMessage:="Title is required.")> _
    Public Title As Object
    ' Require that the MiddleName is not null.
    ' Use standard validation error.
    <Required()> _
    Public MiddleName As Object
End Class
注解
属性 RequiredAttribute 指定验证窗体上的字段时,该字段必须包含值。 如果 属性为 null,则引发验证异常,其中包含空字符串 (“”) ,或仅包含空格字符。
注意
如果 MVC 数据模型或实体分部类包含使用 RequiredAttribute 属性进行注释的字段,但页面不包含 属性,则不会引发错误。 仅对提交到服务器的字段进行验证。
构造函数
| RequiredAttribute() | 初始化 RequiredAttribute 类的新实例。 | 
属性
| AllowEmptyStrings | 获取或设置一个值,该值指示是否允许空字符串。 | 
| ErrorMessage | 获取或设置一条在验证失败的情况下与验证控件关联的错误消息。(继承自 ValidationAttribute) | 
| ErrorMessageResourceName | 获取或设置错误消息资源的名称,在验证失败的情况下,要使用该名称来查找 ErrorMessageResourceType 属性值。(继承自 ValidationAttribute) | 
| ErrorMessageResourceType | 获取或设置在验证失败的情况下用于查找错误消息的资源类型。(继承自 ValidationAttribute) | 
| ErrorMessageString | 获取本地化的验证错误消息。(继承自 ValidationAttribute) | 
| RequiresValidationContext | 获取指示特性是否要求验证上下文的值。(继承自 ValidationAttribute) | 
| TypeId | 在派生类中实现时,获取此 Attribute 的唯一标识符。(继承自 Attribute) | 
方法
| Equals(Object) | 返回一个值,该值指示此实例是否与指定的对象相等。(继承自 Attribute) | 
| FormatErrorMessage(String) | 基于发生错误的数据字段对错误消息应用格式设置。(继承自 ValidationAttribute) | 
| GetHashCode() | 返回此实例的哈希代码。(继承自 Attribute) | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| GetValidationResult(Object, ValidationContext) | 检查指定的值对于当前的验证特性是否有效。(继承自 ValidationAttribute) | 
| IsDefaultAttribute() | 在派生类中重写时,指示此实例的值是否是派生类的默认值。(继承自 Attribute) | 
| IsValid(Object) | 检查必填数据字段的值是否不为空。 | 
| IsValid(Object, ValidationContext) | 根据当前的验证特性来验证指定的值。(继承自 ValidationAttribute) | 
| Match(Object) | 当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。(继承自 Attribute) | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| ToString() | 返回表示当前对象的字符串。(继承自 Object) | 
| Validate(Object, String) | 验证指定的对象。(继承自 ValidationAttribute) | 
| Validate(Object, ValidationContext) | 验证指定的对象。(继承自 ValidationAttribute) | 
显式接口实现
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) | 将一组名称映射为对应的一组调度标识符。(继承自 Attribute) | 
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) | 检索对象的类型信息,然后可以使用该信息获取接口的类型信息。(继承自 Attribute) | 
| _Attribute.GetTypeInfoCount(UInt32) | 检索对象提供的类型信息接口的数量(0 或 1)。(继承自 Attribute) | 
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) | 提供对某一对象公开的属性和方法的访问。(继承自 Attribute) |