asp.net mvc 模型驗證註解,表單提交

1、添加模型javascript

    public class Account
    {
        public int ID { get; set; }

        [Display(Name = "姓名")]    //設置要顯示的字段名 
        [Required(ErrorMessage = "您須要填寫{0}")]  //設置爲必須字段 已經錯誤提示 
        [StringLength(50, MinimumLength = 3)]     //設置最大長度和最小長度 
        public string Name { get; set; }

        [Display(Name = "年齡")]
        [Range(1, 150, ErrorMessage = "年齡填寫不正確!")]  //設置 值範圍 
        public int Age { get; set; }

        [Display(Name = "身高")]
        [Range(typeof(decimal), "50.00", "250.00", ErrorMessage = "身高超出指定範圍")]
        public decimal Height { get; set; }

        [Display(Name = "生日")]
        [DataType(DataType.Date, ErrorMessage = "{0}格式不正確")]  //設置數據類型以及錯誤提示 
        public DateTime Birthday { get; set; }

        [Display(Name = "電話")]
        [Remote("CheckPhone", "Account", ErrorMessage = "{0}已被註冊")]   //在指定的Conteroller中的通道(route)中驗證數據 
        public string Phone { get; set; }

        [Display(Name = "地址")]
        [DataType(DataType.MultilineText)]
        public string Address { get; set; }

        [Display(Name = "電子郵箱")]
        [RegularExpression(@"(\w)+(\.\w+)*@(\w)+((\.\w+)+)", ErrorMessage = "{0}格式不正確")]  //正則驗證 
        public string Email { get; set; }

        [Display(Name = "再次輸入電子郵箱")]
        [Compare("Email", ErrorMessage = "{0}兩次輸入不一致")]   //設置比較兩個字段的值 
        public string EmailConfirm { get; set; }

        [Display(Name = "密碼")]
        [DataType(DataType.Password)]
        public string Password { get; set; }

        [Display(Name = "備用電子郵箱")]
        [DataType(DataType.EmailAddress, ErrorMessage = "{0}格式不正確")]
        public string email_B { get; set; }
    } 
Models

2、添加控制器java

    public class AccountController : Controller
    {
        //
        // GET: /Account/

        public ActionResult Register()
        {
            return View();
        }

        [HttpPost]//主意表單提交默認post,要加上HttpPost特性
        public ActionResult Register(Account model)
        {
            if (ModelState.IsValid)//模型驗證註解驗證是否成功
            {
                //執行其餘驗證...若是驗證不經過則向模型驗證添加錯誤信息:ModelState.AddModelError("key", "錯誤信息");
                
                //若是其餘驗證成功則調用業務邏輯...

                //執行成功則跳轉頁面
                return RedirectToActionPermanent("Index", "Home");
            }
            return View(model);
        }

        //Remote驗證
        [OutputCache(Location = OutputCacheLocation.None, NoStore = true)]   //清除緩存
        public JsonResult CheckPhone(string Phone)
        {
            bool flag = true;//檢測phone的方法調用
            if (flag)
            {
                return Json(true, JsonRequestBehavior.AllowGet);
            }
            return Json(false, JsonRequestBehavior.AllowGet);
        }

    }
Controller

3、添加視圖jquery

@model MvcApplication2.Models.Account

@{
    ViewBag.Title = "Register";
}

<h2>Register</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)
    <fieldset>
        <legend>Account</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Age)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Age)
            @Html.ValidationMessageFor(model => model.Age)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Height)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Height)
            @Html.ValidationMessageFor(model => model.Height)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Birthday)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Birthday)
            @Html.ValidationMessageFor(model => model.Birthday)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Phone)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Phone)
            @Html.ValidationMessageFor(model => model.Phone)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Address)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Address)
            @Html.ValidationMessageFor(model => model.Address)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Email)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Email)
            @Html.ValidationMessageFor(model => model.Email)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.EmailConfirm)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.EmailConfirm)
            @Html.ValidationMessageFor(model => model.EmailConfirm)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Password)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Password)
            @Html.ValidationMessageFor(model => model.Password)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.email_B)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.email_B)
            @Html.ValidationMessageFor(model => model.email_B)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}
View Code

主意,該視圖是使用mvc模板建立,也能夠手動編寫緩存

相關文章
相關標籤/搜索