MVC中的模型注解

authour: chenboyi
updatetime: 2015-04-26 21:28:42
friendly link:  

 

 

 


目录:

1,思维导图

2,内容解析

3,CodeSimple

 


1,思维导图

 

 

2,内容解析

   //模型注解 

      2.1DisplayName(“程序员自定义文字”)配合 @Html.EditorForModel(),  @Html.DisplayNameFor(),               @Html.LabelFor(),@Html.ValidationMessageFor(),@Html.DisplayNameForModel()
 

     2.2Required()非空验证 ,使用步骤如下:

          非侵入式脚本的使用 (作用:用来检查form表单中所有元素值的合法性)
     步骤:
     1、在实体属性上打上 [Required(ErrorMessage = "猪名称不能为空")]
     2、元素要使用@Html.TextBoxFor<> 等来生成
     3、在视图 上添加  @Html.ValidationMessageFor(c => c.name) :用来显示错误提示信息
      2.3、非空验证 引入
     <script src="~/Scripts/jquery-1.7.1.js"></script>
     jquery form表单元素数据合法性检查的插件
    <script src="~/Scripts/jquery.validate.js"></script>
         jquery form表单元素数据合法性检查的插件的非侵入式脚本
    <script src="~/Scripts/jquery.validate.unobtrusive.js"></script>

     2.4、利用@Html. TextBoxFor()方法生成的元素要放到form表单中

      2.5、确认MVC网站中 的web.config中的 <add key="ClientValidationEnabled" value="true" /> value值为true

     2.6、使用验证元素的注意点:
     所有验证的元素必须放到 <form>表单中,并且是通过 submit按钮来提交

      2.7、   [Range(1, 100, ErrorMessage = "id只能取1-100范围中的值")] :进行范围限制
     2.8、[RegularExpression("^[\\u4E00-\\u9FA5\\uF900-\\uFA2D]+$",ErrorMessage="名称只能输入中文")]利用正则表达式来检查元素的值是否满足要求
     2.9、System.ComponentModel.DataAnnotations.Compare ()标示当前属性和程序员指定的另外一个属性的值要保持一致
     2.10、[Remote("checkId", "Home", ErrorMessage = "当前id已经被使用,请换一个", HttpMethod="post")]
       Remote发出一个post的ajax请求 /Home/checkid 如果id存在则返回 “false” 不存在则返回 "true"
     2.11、 [DisplayName("xxx")]:标示一个属性的显示值 ,能够被 @Html.EditorForModel(),  @Html.DisplayNameFor(), @Html.LabelFor()解析出来
 
3,CodeSimple
 1 //ZJController.csusing MVC知识点.Models;

 2 using System;

 3 using System.Collections.Generic;

 4 using System.Linq;

 5 using System.Web;

 6 using System.Web.Mvc;

 7 

 8 namespace MVC知识点.Controllers

 9 {

10     /// <summary>

11     /// 负责演示模型注解和验证的控制器

12     /// </summary>

13     public class ZJController : Controller

14     {

15         //

16         // GET: /C02ZJ/

17 

18         [HttpGet]

19         public ActionResult Add()

20         {

21             Pig pig = new Pig() { Name = "八戒", Age = 500 };

22             return View(pig);

23         }

24 

25         [HttpPost]

26         public ActionResult Add(Pig model)

27         {

28             //验证model参数中的属性值是否全部合法

29             if (ModelState.IsValid == false)

30             {

31                 return View();

32             }

33             return new EmptyResult();

34         }

35 

36         /// <summary>

37         /// 负责检查猪的名字是否已经存在,此方法被ajax请求

38         /// </summary>

39         /// <returns></returns>

40         public ActionResult CheckName()

41         {

42             //1.0 获取当前要检查的名称值

43             string name = Request.Form["Name"];

44 

45             if (name == "八戒")

46             {

47                //当name的值等于八戒,则告诉用户此用户名已经使用

48                 return Content("false");

49             }

50             else

51             {

52                 //表示此用户名可以使用

53                 return Content("true");

54             }

55         }

56     }

57 }

 

//pig类
1
using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 6 namespace MVC知识点.Models 7 { 8 using System.ComponentModel; 9 using System.ComponentModel.DataAnnotations; 10 using System.Web.Mvc; 11 12 public class Pig 13 { 14 [DisplayName("猪名称")] 15 [Required(ErrorMessage = "猪名称非空")] 16 [StringLength(10, MinimumLength = 1, ErrorMessage = "猪名称的长度介于1到10之间")] 17 [Remote("CheckName", "C02ZJ", HttpMethod = "post", ErrorMessage = "用户名称已存在,请换一个")] 18 public string Name { get; set; } 19 [DisplayName("猪年龄"), Required(ErrorMessage = "猪年龄非空"), Range(1, 500, ErrorMessage = "猪年龄只能介于1到500之间")] 20 public int Age { get; set; } 21 22 public string Pwd { get; set; } 23 [System.ComponentModel.DataAnnotations.Compare("Pwd", ErrorMessage = "确认密码必须与原始密码保持一致")] 24 public string ConfrimPwd { get; set; } 25 26 [RegularExpression("^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$", ErrorMessage = "Email格式不合法")] 27 public string Email { get; set; } 28 } 29 }

 

 1 <!-- Add.cshtml -->

 2 @model MVC知识点.Models.Pig

 3 

 4 @{

 5     Layout = null;

 6 }

 7 

 8 <!DOCTYPE html>

 9 

10 <html>

11 <head>

12     <meta name="viewport" content="width=device-width" />

13     <title>Add</title>

14     <script src="~/Scripts/jquery-1.7.1.js"></script>

15     <script src="~/Scripts/jquery.validate.js"></script>

16     <script src="~/Scripts/jquery.validate.unobtrusive.js"></script>

17 </head>

18 <body>

19     <div>

20 

21         <form method="post">

22  @Html.DisplayNameFor(c => c.Name):

23             @*<input type="text" name="Name" value="@Model.Name" /><br />*@

24  @Html.TextBoxFor(c => c.Name)

25  @Html.ValidationMessageFor(c => c.Name)

26             <br />

27  @Html.LabelFor(c => c.Age):

28             @*<input type="text" name="Age" value="@Model.Age" />*@

29  @Html.TextBoxFor(c => c.Age)

30  @Html.ValidationMessageFor(c => c.Age)

31             <br />

32 

33             @Html.PasswordFor(c => c.Pwd)<br />

34 

35  @Html.PasswordFor(c => c.ConfrimPwd)

36  @Html.ValidationMessageFor(c => c.ConfrimPwd)

37             <br />

38  @Html.TextBoxFor(c => c.Email)

39  @Html.ValidationMessageFor(c => c.Email)

40             <br />

41             <input type="submit" value="提交" />

42         </form>

43     </div>

44 </body>

45 </html>

 

你可能感兴趣的:(mvc)