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
- {
- /// <summary>
- /// 姓名验证,只能是中文
- /// </summary>
- /// <param name="ExamineeName">姓名</param>
- /// <returns></returns>
- 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")]//服务端参与的验证(注意:ExamineeNameCheck方法的参数名必须叫XM)
- //[RegularExpression(@"^[\u4e00-\u9fa5]{0,}$",ErrorMessage = "只能输入汉字")]//正则表达式
- 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)//模型状态字典实例有效
- {
- //一、验收数据的合法性
- //1、AJAX实现客户端数据验证(在数据被送到后台前,我们应该先进行一遍验证,这样可以节约很多资源)
- //2、同步方式完成数据验证
- if (String.IsNullOrEmpty(person.XM))
- {
- ViewData.ModelState.AddModelError("XM", "姓名不能为空!");
- return Index();//返回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>
- }