MVC数据验证Model Validation

Required必须项验证属性

[Required]
public string FirstName { get; set; }
        //ID编号
       [ScaffoldColumn(false)]
       [Required(AllowEmptyStrings = false, ErrorMessage = "用户ID不能为空")]
       [Display(Name = "记录编号", Order = 20000)]
       public int ID { get; set; }

StringLength长度

[Required]
[StringLength(160)]
public string LastName { get; set; }
[Required]
[StringLength(160, MinimumLength=3)]
public string FirstName { get; set; }

RegularExpression正则表达式

[RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}")]
public string Email { get; set; }
     [Required(AllowEmptyStrings = false, ErrorMessage = "邮箱必填")]
     [RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9]+.[A-Za-z]{2,4}", ErrorMessage = "{0}的格式不正确")]
     public string Email { get; set; }

匹配验证

[RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}")]
public string Email { get; set; }
[Compare("Email")]
public string EmailConfirm { get; set; }

Compare  比较两个字段值是否相同。

 Range数字范围

[Range(35,44)]
public int Age { get; set; }
[Range(typeof(decimal), "0.00", "49.99")]
public decimal Price { get; set; }

Custom Error Messages and Localization自定义错误消息和本地化

[RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}",
ErrorMessage="Email doesn't look like a valid email address.")]
public string Email { get; set; }
[Required(ErrorMessage="Your last name is required")]
[StringLength(160, ErrorMessage="Your last name is too long")]
public string LastName { get; set; }
    @Html.EditorFor(model => model.Email)
    @Html.ValidationMessageFor(model => model.Email)

Display

[Required]
[StringLength(160, MinimumLength=3)]
[Display(Name="First Name")]
public string FirstName { get; set; }
        [Display(Name = "身份证号码")]
        [RegularExpression(@"d{17}[d|x]|d{15}", ErrorMessage = "身份证号码格式错误")]
                <div class="editor-label">
                     @Html.LabelFor(t => t.IdentityNo)
                </div>
                 <div class="editor-field">
                     @Html.EditorFor(model => model.IdentityNo)
                    @Html.ValidationMessageFor(model => model.IdentityNo)
                </div>    

自动生成编辑页面

<fieldset>
<legend>Shipping Information</legend>
@Html.EditorForModel()
</fieldset>

  

 <div>
        @using (Html.BeginForm())
        {
            @Html.ValidationSummary(true)
            <fieldset>
                <legend>UserInfo</legend>   
                <div class="editor-label">
                    @Html.LabelFor(t => t.UserPassword)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.UserPassword)
                    @Html.ValidationMessageFor(model => model.UserPassword)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(t => t.IdentityNo)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.IdentityNo)
                    @Html.ValidationMessageFor(model => model.IdentityNo)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(t => t.Email)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Email)
                    @Html.ValidationMessageFor(model => model.Email)
                </div> 
                <div class="editor-label">
                    @Html.LabelFor(t => t.Age)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Age)
                   @Html.ValidationMessageFor(model => model.Age)
                </div> 
                <div class="editor-label">
                    @Html.LabelFor(t => t.Money)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Money)
                    @Html.ValidationMessageFor(model => model.Money)
                </div> 
                 <div class="editor-label">
                    @Html.LabelFor(t => t.TEmail)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.TEmail)
                    @Html.ValidationMessageFor(model => model.TEmail)
                </div> 
                @Html.EditorForModel() 
            </fieldset>
            <input type="submit" value="提交" />
        }
    </div>

隐藏属性ScaffoldColumn,使用EditorForModel生效

[ScaffoldColumn(false)]
public string Username { get; set; }

如果设置为false,则该字段不会在View层显示,里面定义的验证也不会生效。

DisplayFormat格式化已短时间形式显示显示

[DisplayFormat(ApplyFormatInEditMode=true, DataFormatString="{0:d}")]
public decimal Time{ get; set; }
以货币格式显示数值
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")]
public decimal Pay{ get; set; }

ReadOnly只读属性,在页面上可以显示但无法将值传入控制器

[ReadOnly(true)]
public decimal Total { get; set; }

DataType数据类型

[Required]
[DataType(DataType.Password)]
[Display(Name="Password")]
public string Password { get; set; }

 

 

原文地址:https://www.cnblogs.com/shy1766IT/p/5080972.html