HtmlHelper用法大全3:Html.LabelFor、Html.EditorFor、Html.RadioButtonFor、Html.CheckBoxFor


 @Html.***For:為由 指定表示式 表示對象中的 每個屬性,返回對應html

示例效果:

 

一、設置通用的驗證方法

         Models層

[csharp]  view plain copy
  1. public class MyStringIsChineseAttribute: ValidationAttribute  
  2. {  
  3.     #region 驗證中文  
  4.     private bool _myreturn = false;  
  5.     public bool myNullDefVal  
  6.     {  
  7.         get { return _myreturn; }  
  8.         set { _myreturn = value; }  
  9.   
  10.     }  
  11.     //覆蓋父類方法(確定對象的指定值是否有效)  
  12.     public override bool IsValid(object value)  
  13.     {  
  14.         if (value == nullreturn _myreturn;  
  15.         return Regex.IsMatch(value.ToString(), @"^[\u4e00-\u9fa5]{0,}$", RegexOptions.IgnoreCase);  
  16.     }  
  17.     //覆蓋父類方法(基於發生錯誤的數據字段對錯誤消息應用格式設置)  
  18.     public override string FormatErrorMessage(string name)  
  19.     {  
  20.         return "非中文字符!";  
  21.     }  
  22.     #endregion  
  23. }  


        Controller層

[csharp]  view plain copy
  1. public class MyValidDataController : Controller  
  2. {  
  3.     /// <summary>  
  4.     /// 姓名驗證,只能是中文  
  5.     /// </summary>  
  6.     /// <param name="ExamineeName">姓名</param>  
  7.     /// <returns></returns>  
  8.     public JsonResult ExamineeNameCheck(string XM)  
  9.     {  
  10.         MyStringIsChineseAttribute ff = new MyStringIsChineseAttribute();  
  11.         bool myidexist = ff.IsValid(XM);  
  12.         if (!myidexist)  
  13.         {  
  14.             return Json("姓名只能是中文!", JsonRequestBehavior.AllowGet);  
  15.         }  
  16.         else  
  17.         {  
  18.             return Json(true, JsonRequestBehavior.AllowGet);  
  19.         }  
  20.     }  
  21.   
  22. }  


 

 

二、Person類(Models層)

[csharp]  view plain copy
  1. public class Person  
  2. {  
  3.     #region  屬性定義  
  4.     [Display(Name = "姓名")]  
  5.     [Required(ErrorMessage="{0}不能為空.")]  
  6.     [StringLength(4, MinimumLength = 2, ErrorMessage = " {0} 最少 {2} 字符,最多{1}字符。")]  
  7.     [Remote("ExamineeNameCheck""MyValidData")]//服務端參與的驗證(注意:ExamineeNameCheck方法的參數名必須叫XM)  
  8.     //[RegularExpression(@"^[\u4e00-\u9fa5]{0,}$",ErrorMessage = "只能輸入漢字")]//正則表達式  
  9.     public string XM{ getset; }  
  10.   
  11.     [Display(Name = "性別")]  
  12.     public bool XB { getset; }  
  13.   
  14.     [Display(Name = "愛好1")]  
  15.     public bool AH1 { getset; }  
  16.   
  17.     [Display(Name = "愛好2")]  
  18.     public bool AH2 { getset; }  
  19.   
  20.     [Display(Name = "學歷")]  
  21.     public string XL { getset; }  
  22.   
  23.     [Display(Name = "備注")]  
  24.     public string BZ { getset; }  
  25.     #endregion  
  26. }  

 

三、Controller層

[csharp]  view plain copy
  1.     public class TestController : Controller  
  2.     {  
  3.         public ActionResult Index()  
  4.         {  
  5.             Person person = new Person();  
  6.             person.XM = "小張";  
  7.             person.XB = false;  
  8.             person.AH2 = true;  
  9.   
  10.             List<SelectListItem> lists = new List<SelectListItem>  
  11.             {  
  12.                 new SelectListItem{Text="大學",Value="大學"},  
  13.                 new SelectListItem{Text="高中",Value="高中"},  
  14.                 new SelectListItem{Text="初中",Value="初中"}  
  15.             };  
  16.             ViewData["XlList"] = lists;  
  17.             person.XL = "高中";  
  18.   
  19.             person.BZ = "備注";  
  20.             return View(person);  
  21.         }  
  22.   
  23.         [HttpPost]  
  24.         public ActionResult Index(Person person,FormCollection fc)  
  25.         {  
  26.             //注意防止頁面反復提交  
  27.             string str = "";  
  28.             if (ModelState.IsValid)//模型狀態字典實例有效  
  29.             {  
  30.                //一、驗收數據的合法性  
  31.                //1、AJAX實現客戶端數據驗證(在數據被送到后台前,我們應該先進行一遍驗證,這樣可以節約很多資源)                
  32.                //2、同步方式完成數據驗證  
  33.                if (String.IsNullOrEmpty(person.XM))  
  34.                {  
  35.                   ViewData.ModelState.AddModelError("XM""姓名不能為空!");  
  36.                   return Index();//返回Index方法  
  37.                }                  
  38.                 str += "姓名:" + person.XM + "<br>";  
  39.                 str += "性別:" + person.XB + "<br>";  
  40.                 str += "愛好1:" + person.AH1 + "<br>";  
  41.                 str += "愛好2:" + person.AH2 + "<br>";  
  42.                 str += "學歷:" + person.XL + "<br>";  
  43.                 str += "備注:" + person.BZ + "<br>";  
  44.             }  
  45.             return Content(str);  
  46.         }  
  47.     }  


 

四、Views層

[html]  view plain copy
  1. @model MvcApplication4.Models.Person  
  2.   
  3. @{  
  4.     ViewBag.Title = "Index";  
  5.     Layout = "~/Views/Shared/_Layout.cshtml";  
  6. }  
  7.   
  8. <h2>編輯用戶信息</h2>  
  9.   
  10. <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>  
  11. <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>  
  12.   
  13.   
  14. @using (Html.BeginForm()) {  
  15.     @Html.ValidationSummary(true)  
  16.   
  17.   
  18.         <div>  
  19.         @Html.LabelFor(model => model.XM)  
  20.         @Html.EditorFor(model => model.XM)  
  21.         @Html.ValidationMessageFor(model => model.XM)  
  22.         </div>  
  23.         <div>  
  24.         @Html.LabelFor(model=>model.XB)  
  25.         @Html.RadioButtonFor(model => model.XB, true)男  
  26.         @Html.RadioButtonFor(model => model.XB, false)女  
  27.         @Html.ValidationMessageFor(model => model.XB)  
  28.         </div>  
  29.         <div>  
  30.         @Html.LabelFor(model => model.AH1)  
  31.         @Html.CheckBoxFor(model => model.AH1)  
  32.   
  33.         @Html.LabelFor(model => model.AH2)  
  34.         @Html.CheckBoxFor(model=>model.AH2)  
  35.         </div>  
  36.         <div>  
  37.          @Html.LabelFor(model => model.XL)  
  38.         @Html.DropDownListFor(model => model.XL, ViewData["XlList"] as IEnumerable<SelectListItem>)  
  39.         </div>  
  40.         <div>  
  41.          @Html.LabelFor(model => model.BZ)  
  42.         @Html.TextAreaFor(model=>model.BZ,3,30,null)  
  43.         </div>  
  44.         <div>  
  45.          <input type="submit" value="保存" name="tj"/>  
  46.         </div>  
  47. }  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM