• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
博雅居
要想跟上时代,就得不断学习!
博客园    首页    新随笔    联系   管理    订阅  订阅
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 中)

继承层次结构
 
 
System.Object
System.Attribute
System.ComponentModel.DataAnnotations.ValidationAttribute
System.ComponentModel.DataAnnotations.RequiredAttribute

语法 
[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;

}

 

 

If opportunity doesn’t knock, build a door
posted on 2015-11-05 10:36  博雅居  阅读(1534)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3