zoukankan      html  css  js  c++  java
  • Asp.net MVC 简单的Model验证

    一.什么是Model验证

    Asp.Net MVC 采用Model绑定为目标Action生成相应的参数列表,但是在Action方法执行前,还要对绑定的参数进行验证,我们就把这种带参数的验证称之为Model验证。

    二.正文

       下面直接上简单Demo操作

       第一步:模型验证就要建Model 并加属性验证 

     1  public class Person
     2     {
     3         [Key]
     4         public int pid { get; set; } 
     5 
     6         [Required(ErrorMessage = "必填")]
     7         [StringLength(20, MinimumLength = 4, ErrorMessage = "{2}到{1}个字符")]
     8         [Display(Name = "用户名")]
     9         public string pname { get; set; }
    10 
    11         [Required(ErrorMessage = "必填")]
    12         [Display(Name = "生日")]
    13         [DataType(DataType.DateTime)]
    14         public DateTime pbirthday { get; set; }
    15 
    16         [Required(ErrorMessage = "必填")]
    17         [Display(Name = "地址")]
    18         [StringLength(20, MinimumLength = 4, ErrorMessage = "{2}到{1}个字符")]
    19         public string paddress { get; set; } 
    20 
    21         [Required(ErrorMessage = "必填")]
    22         [Display(Name = "邮箱")]
    23         [DataType(DataType.EmailAddress)]
    24         public string Email { get; set; }
    25     }
    View Code

       第二步:配置模型验证需要的js

        首先打开  网站目录下App_Start文件夹----->BundleConfig.cs   (如果有的话就不用配置)

    1  bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
    2                         "~/Scripts/jquery-{version}.js"));
    3 
    4             bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
    5                         "~/Scripts/jquery.unobtrusive*",
    6                         "~/Scripts/jquery.validate*"));
    View Code

        第三步:在布局页中进行引入

     1 <!DOCTYPE html>
     2 <html>
     3 <head>
     4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
     5     <meta charset="utf-8" />
     6     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     7     <title>@ViewBag.Title - 我的 ASP.NET 应用程序</title>
     8     @Styles.Render("~/Content/css") 
     9     @Scripts.Render("~/bundles/jquery", "~/bundles/modernizr", "~/bundles/jqueryval")
    10 </head>
    11 <body>
    12     <div class="navbar navbar-inverse navbar-fixed-top">
    13         <div class="container">
    14             <div class="navbar-header">
    15                 <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
    16                     <span class="icon-bar"></span>
    17                     <span class="icon-bar"></span>
    18                     <span class="icon-bar"></span>
    19                 </button>
    20                 @Html.ActionLink("应用程序名称", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
    21             </div>
    22             <div class="navbar-collapse collapse">
    23                 <ul class="nav navbar-nav">
    24                     <li>@Html.ActionLink("主页", "Index", "Home")</li>
    25                     <li>@Html.ActionLink("关于", "About", "Home")</li>
    26                     <li>@Html.ActionLink("联系方式", "Contact", "Home")</li>
    27                 </ul>
    28                 @Html.Partial("_LoginPartial")
    29             </div>
    30         </div>
    31     </div>
    32     <div class="container body-content">
    33         @RenderBody()
    34         <hr />
    35         <footer>
    36             <p>&copy; @DateTime.Now.Year - 我的 ASP.NET 应用程序</p>
    37         </footer>
    38     </div> 
    39     @Scripts.Render("~/bundles/bootstrap")
    40     @RenderSection("scripts", required: false)
    41 </body>
    42 </html>
    View Code

       第四步:视图部分

     1 @model wahaha.Models.Person
     2 @{
     3     ViewBag.Title = "Index"; 
     4 }
     5 
     6 <h2>Index</h2>
     7 @using (Html.BeginForm())
     8 {
     9     <div class="form-horizontal">
    10         <h4>用户注册</h4>
    11         <hr />
    12         @Html.ValidationSummary(true) 
    13       <div class="form-group">
    14             @Html.LabelFor(model => model.pname, new { @class = "col-md-2 control-label" })
    15             <div class="col-md-10">
    16                 @Html.EditorFor(model => model.pname)
    17                 @Html.ValidationMessageFor(model => model.pname)
    18             </div>
    19         </div> 
    20 
    21         <div class="form-group">
    22             @Html.LabelFor(model => model.pbirthday, new { @class = "col-md-2 control-label" })
    23             <div class="col-md-10">
    24                 @Html.EditorFor(model => model.pbirthday)
    25                 @Html.ValidationMessageFor(model => model.pbirthday)
    26             </div>
    27         </div>
    28 
    29         <div class="form-group">
    30             @Html.LabelFor(model => model.paddress,new { @class = "col-md-2 control-label" })
    31             <div class="col-md-10">
    32                 @Html.EditorFor(model => model.paddress)
    33                 @Html.ValidationMessageFor(model => model.paddress)
    34             </div>
    35         </div>
    36         <div class="form-group">
    37             @Html.LabelFor(model => model.Email,new { @class = "col-md-2 control-label" })
    38             <div class="col-md-10">
    39                 @Html.EditorFor(model => model.Email)
    40                 @Html.ValidationMessageFor(model => model.Email)
    41             </div>
    42         </div> 
    43         <hr />
    44         <div class="form-group">
    45             <div class="col-md-offset-2 col-md-10">
    46                 <input type="submit" value="注册" class="btn btn-default" />
    47             </div>
    48         </div>
    49     </div>
    50 }
    View Code

        第五步:运行项目

       

          

           一个简单的demo完成。

  • 相关阅读:
    开源库dlib的安装与编译-CMake
    Python的zip函数
    matlab读写视频VideoReader/VideoWriter
    批量分割视频opencv
    批量重命名文件
    OpenCV代码提取:遍历指定目录下指定文件的实现
    第48课 函数设计原则(完)
    在Qt中如何使用QtDesigner创建的UI文件(一) (转)
    qt ui程序使用Linux的文件操作open、close (转)
    进程间通信IPC之--无名管道(pipe)和有名管道(fifo)(转)
  • 原文地址:https://www.cnblogs.com/w1120/p/4725796.html
Copyright © 2011-2022 走看看