1个示例 学会 mvc 常用标签_.NET_编程开发_程序员俱乐部
    
    
    
    
    
        
    
    
    
 
    
        
            
            
            
                
                    
                        1个示例 学会 mvc 常用标签
                
                
                
                    
                        - 摘要:HtmlHelper用法大全3:Html.LabelFor、Html.EditorFor、Html.RadioButtonFor、Html.CheckBoxFor@Html.***For:为由指定表示式表示对象中的每个属性,返回对应html示例效果:一、设置通用的验证方法Models层publicclassMyStringIsChineseAttribute:ValidationAttribute{#region验证中文privatebool_myreturn=false
- 标签:MVC 常用 
 
                
                    
                    class="block_title">HtmlHelper用法大全3:Html.LabelFor、Html.EditorFor、Html.RadioButtonFor、Html.CheckBoxFor
 @Html.***For:为由
指定表示式 表示对象中的 每个属性,返回对应html
示例效果:

 
一、设置通用的验证方法
         Models层
- public class MyStringIsChineseAttribute: ValidationAttribute  
- {  
-     #region 验证中文  
-     private bool _myreturn = false;  
-     public bool myNullDefVal  
-     {  
-         get { return _myreturn; }  
-         set { _myreturn = value; }  
-   
-     }  
-     
-     public override bool IsValid(object value)  
-     {  
-         if (value == null) return _myreturn;  
-         return Regex.IsMatch(value.ToString(), @"^[\u4e00-\u9fa5]{0,}$", RegexOptions.IgnoreCase);  
-     }  
-     
-     public override string FormatErrorMessage(string name)  
-     {  
-         return "非中文字符!";  
-     }  
-     #endregion  
- }  
        Controller层
- public class MyValidDataController : Controller  
- {  
-     
-     
-     
-     
-     
-     public JsonResult ExamineeNameCheck(string XM)  
-     {  
-         MyStringIsChineseAttribute ff = new MyStringIsChineseAttribute();  
-         bool myidexist = ff.IsValid(XM);  
-         if (!myidexist)  
-         {  
-             return Json("姓名只能是中文!", JsonRequestBehavior.AllowGet);  
-         }  
-         else  
-         {  
-             return Json(true, JsonRequestBehavior.AllowGet);  
-         }  
-     }  
-   
- }  
 
 
二、Person类(Models层)
- public class Person  
- {  
-     #region  属性定义  
-     [Display(Name = "姓名")]  
-     [Required(ErrorMessage="{0}不能为空.")]  
-     [StringLength(4, MinimumLength = 2, ErrorMessage = " {0} 最少 {2} 字符,最多{1}字符。")]  
-     [Remote("ExamineeNameCheck", "MyValidData")]
-     
-     public string XM{ get; set; }  
-   
-     [Display(Name = "性别")]  
-     public bool XB { get; set; }  
-   
-     [Display(Name = "爱好1")]  
-     public bool AH1 { get; set; }  
-   
-     [Display(Name = "爱好2")]  
-     public bool AH2 { get; set; }  
-   
-     [Display(Name = "学历")]  
-     public string XL { get; set; }  
-   
-     [Display(Name = "备注")]  
-     public string BZ { get; set; }  
-     #endregion  
- }  
 
三、Controller层
-     public class TestController : Controller  
-     {  
-         public ActionResult Index()  
-         {  
-             Person person = new Person();  
-             person.XM = "小张";  
-             person.XB = false;  
-             person.AH2 = true;  
-   
-             List<SelectListItem> lists = new List<SelectListItem>  
-             {  
-                 new SelectListItem{Text="大学",Value="大学"},  
-                 new SelectListItem{Text="高中",Value="高中"},  
-                 new SelectListItem{Text="初中",Value="初中"}  
-             };  
-             ViewData["XlList"] = lists;  
-             person.XL = "高中";  
-   
-             person.BZ = "备注";  
-             return View(person);  
-         }  
-   
-         [HttpPost]  
-         public ActionResult Index(Person person,FormCollection fc)  
-         {  
-             
-             string str = "";  
-             if (ModelState.IsValid)
-             {  
-                
-                
-                
-                if (String.IsNullOrEmpty(person.XM))  
-                {  
-                   ViewData.ModelState.AddModelError("XM", "姓名不能为空!");  
-                   return Index();
-                }                  
-                 str += "姓名:" + person.XM + "<br>";  
-                 str += "性别:" + person.XB + "<br>";  
-                 str += "爱好1:" + person.AH1 + "<br>";  
-                 str += "爱好2:" + person.AH2 + "<br>";  
-                 str += "学历:" + person.XL + "<br>";  
-                 str += "备注:" + person.BZ + "<br>";  
-             }  
-             return Content(str);  
-         }  
-     }  
 
四、Views层
- @model MvcApplication4.Models.Person  
-   
- @{  
-     ViewBag.Title = "Index";  
-     Layout = "~/Views/Shared/_Layout.cshtml";  
- }  
-   
- <h2>编辑用户信息</h2>  
-   
- <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>  
- <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>  
-   
-   
- @using (Html.BeginForm()) {  
-     @Html.ValidationSummary(true)  
-   
-   
-         <div>  
-         @Html.LabelFor(model => model.XM)  
-         @Html.EditorFor(model => model.XM)  
-         @Html.ValidationMessageFor(model => model.XM)  
-         </div>  
-         <div>  
-         @Html.LabelFor(model=>model.XB)  
-         @Html.RadioButtonFor(model => model.XB, true)男  
-         @Html.RadioButtonFor(model => model.XB, false)女  
-         @Html.ValidationMessageFor(model => model.XB)  
-         </div>  
-         <div>  
-         @Html.LabelFor(model => model.AH1)  
-         @Html.CheckBoxFor(model => model.AH1)  
-   
-         @Html.LabelFor(model => model.AH2)  
-         @Html.CheckBoxFor(model=>model.AH2)  
-         </div>  
-         <div>  
-          @Html.LabelFor(model => model.XL)  
-         @Html.DropDownListFor(model => model.XL, ViewData["XlList"] as IEnumerable<SelectListItem>)  
-         </div>  
-         <div>  
-          @Html.LabelFor(model => model.BZ)  
-         @Html.TextAreaFor(model=>model.BZ,3,30,null)  
-         </div>  
-         <div>  
-          <input type="submit" value="保存" name="tj"/>  
-         </div>  
- }