标签:
authour: | chenboyi |
updatetime: | 2015-04-26 21:28:42 |
friendly link: |
目录:
//模型注解
2.2、Required()非空验证 ,使用步骤如下:
非侵入式脚本的使用 (作用:用来检查form表单中所有元素值的合法性)步骤:1、在实体属性上打上 [Required(ErrorMessage = "猪名称不能为空")]2、元素要使用@Html.TextBoxFor<> 等来生成3、在视图 上添加 @Html.ValidationMessageFor(c => c.name) :用来显示错误提示信息
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>
标签:
原文地址:http://www.cnblogs.com/chenboyi081/p/4458486.html