@Html.***For:爲由
指定表示式 表示對象中的 每一個屬性,返回對應html
示例效果:javascript

1、設置通用的驗證方法html
Models層java
- 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層jquery
- 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);
- }
- }
-
- }
2、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
- }
3、Controller層ide
- 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);
- }
- }
4、Views層post
- @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>
- }