asp.net mvc 模型验证注解,表单提交

一、添加模型

    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

二、添加控制器

    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

三、添加视图

@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模板创建,也可以手动编写

原文地址:https://www.cnblogs.com/aweifly/p/3433346.html