MVC自带表单效验

 

废话不说,直接上代码,一看就明白 源地址:http://www.cnblogs.com/sixiangqimeng/p/3561313.html

  [StringLength(5,ErrorMessage = "*长度小于5")]
  [Required(ErrorMessage = "*必填啊!")]
  [Required(ErrorMessage = "*")]
  [RegularExpression(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$")]
   [Range(18,110,ErrorMessage = "长度")]

第一个就是长度效验,第二个是必填效验,后面俩一个是正则,一个是长度,这四个效验,基本已经解决了90%的效验,剩下的在结合jQuery进行效验就行了,类上需要如下

MVC自带表单效验
 public class Student
    {
        //StringLength,Required,RegularExpression,Range

        [StringLength(5,ErrorMessage = "*长度小于5")]
        [Required(ErrorMessage = "*必填啊!")]
        public string   StuName { get; set; }


        [Required(ErrorMessage = "*")]
        [RegularExpression(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$")]
        public string Mail { get; set; }

        [Range(18,110,ErrorMessage = "你是外星人吧")]
        public int  Age { get; set; }



    }
MVC自带表单效验

前台编写方法 源地址:http://www.cnblogs.com/sixiangqimeng/p/3561313.html

MVC自带表单效验
 
<script src="../../Scripts/jquery-1.7.1.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.validate.js" type="text/javascript"></script>

@using (Html.BeginForm("get","Default1","post"))
 {
 <div class="editor-label">
            </div>
            <div class="editor-field">
               @( Html.EditorFor(model => model.StuName))
               @( Html.ValidationMessageFor(model => model.StuName))
              
 </div>
         <input type="submit" value="Create" />
     
 }
MVC自带表单效验

后台效验就可以使用  if (ModelState.IsValid)来进行效验。

MVC自带表单效验

上一篇:Eclipse导入git上的maven web项目 部署 - lpshou


下一篇:Raspberry Pi使用USB摄像头远程监控