zoukankan      html  css  js  c++  java
  • asp.net Mvc 的自定义模型绑定

    View Code
     1 <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<自定义绑定模型.Models.User>" %>
     2 
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     4 <html xmlns="http://www.w3.org/1999/xhtml">
     5 <head runat="server">
     6     <title>Index</title>
     7 </head>
     8 <body>
     9     <% using (Html.BeginForm())
    10        {%>
    11     <%: Html.ValidationSummary(true) %>
    12     <fieldset>
    13         <legend>Fields</legend>
    14         <div class="editor-label">
    15             <%: Html.LabelFor(model => model.UserName) %>
    16         </div>
    17         <div class="editor-field">
    18             <%: Html.TextBoxFor(model => model.UserName) %>
    19             <%: Html.ValidationMessageFor(model => model.UserName) %>
    20         </div>
    21         <div class="editor-label">
    22             <%: Html.LabelFor(model => model.Password) %>
    23         </div>
    24         <div class="editor-field">
    25             <%: Html.TextBoxFor(model => model.Password) %>
    26             <%: Html.ValidationMessageFor(model => model.Password) %>
    27         </div>
    28         <div class="editor-label">
    29             <%: Html.LabelFor(model => model.Email) %>
    30         </div>
    31         <div class="editor-field">
    32             <%: Html.TextBoxFor(model => model.Email) %>
    33             <%: Html.ValidationMessageFor(model => model.Email) %>
    34         </div>
    35         <div class="editor-label">
    36             <%: Html.LabelFor(model => model.Personal.Age) %>
    37         </div>
    38         <div class="editor-field">
    39             <%: Html.TextBoxFor(model => model.Personal.Age)%>
    40             <%: Html.ValidationMessageFor(model => model.Personal.Age)%>
    41         </div>
    42         <div class="editor-label">
    43             <%: Html.LabelFor(model => model.Personal.Gender) %>
    44         </div>
    45         <%--这里是定义了一个变量用来存取下拉列表里面的选择项gender--%>
    46         <% var gender = new List<SelectListItem>{
    47                new SelectListItem{Text="请选择",Value="-1",Selected=true},
    48                new SelectListItem {Text="",Value="0"},
    49                new SelectListItem{Text="",Value="1"}};%>
    50         <div class="editor-field">
    51             <%: Html.DropDownListFor(model => model.Personal.Gender,gender)%>
    52             <%: Html.ValidationMessageFor(model => model.Personal.Age)%>
    53         </div>
    54         <div class="editor-label">
    55             <%: Html.LabelFor(model => model.Personal.RealName) %>
    56         </div>
    57         <div class="editor-field">
    58             <%: Html.TextBoxFor(model => model.Personal.RealName)%>
    59             <%: Html.ValidationMessageFor(model => model.Personal.RealName)%>
    60         </div>
    61         <p>
    62             <input type="submit" value="Create" />
    63         </p>
    64     </fieldset>
    65     <% } %>
    66     <div>
    67         <%: Html.ActionLink("Back to List", "Index") %>
    68     </div>
    69 </body>
    70 </html>

    在模型绑定中我们可以自定义绑定内容和绑定方法类,下面我们来学习下这个东东。。。。。。。。。。

    代码说明:

    我们在定义绑定模型中,有2中绑定方法:
    方法一:给controller中的方法参数添加特性
      [AcceptVerbs(HttpVerbs.Post)]
            public ActionResult Index([Bind(Include = "Personal,Password")]User user)
            {
                return View();
            }
    方法二:给Model中的类添加特性

    //[Bind(Include = "Personal,Password")]//自定义绑定限制,这个是类的方法绑定限制
        public class User
        {

            public string UserName { get; set; }

            public string Password { get; set; }

            public string Email { get; set; }

            public Personal Personal { get; set; }
        }

    下面我来看看案例项目:

    这个是MOdel

    user
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Web;
     5 using System.Web.Mvc;
     6 namespace 自定义绑定模型.Models
     7 {
     8     public enum Gender
     9     {
    10         Male = 1,
    11         Female = 0
    12     }
    13     //[Bind(Include = "Personal,Password")]//自定义绑定限制,这个是类的方法绑定限制
    14     public class User
    15     {
    16 
    17         public string UserName { get; set; }
    18 
    19         public string Password { get; set; }
    20 
    21         public string Email { get; set; }
    22 
    23         public Personal Personal { get; set; }
    24     }
    25 
    26     public class Personal
    27     {
    28         public string RealName { get; set; }
    29 
    30         public int Age { get; set; }
    31 
    32         public Gender Gender { get; set; }
    33     }
    34 }

    controller:

    Controller
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Web;
     5 using System.Web.Mvc;
     6 using 自定义绑定模型.Models;
     7 namespace 自定义绑定模型.Controllers
     8 {
     9     public class HomeController : Controller
    10     {
    11         //
    12         // GET: /Home/
    13 
    14         public ActionResult Index()
    15         {
    16             return View();
    17         }
    18         [AcceptVerbs(HttpVerbs.Post)]
    19         public ActionResult Index([Bind(Include = "Personal,Password")]User user)
    20         {
    21             return View();
    22         }
    23         /*说明:这里使用了自定义的绑定方法。我们给方法中的参数做了限制。
    24          * [Bind(Include = "Personal,Password")]这个特性是将user中的属性只给Personal,Password
    25          * 的属性绑定值,其他的都不绑定
    26          */
    27     }
    28 }

    View:

    View
     1 <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<自定义绑定模型.Models.User>" %>
     2 
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     4 <html xmlns="http://www.w3.org/1999/xhtml">
     5 <head runat="server">
     6     <title>Index</title>
     7 </head>
     8 <body>
     9     <% using (Html.BeginForm())
    10        {%>
    11     <%: Html.ValidationSummary(true) %>
    12     <fieldset>
    13         <legend>Fields</legend>
    14         <div class="editor-label">
    15             <%: Html.LabelFor(model => model.UserName) %>
    16         </div>
    17         <div class="editor-field">
    18             <%: Html.TextBoxFor(model => model.UserName) %>
    19             <%: Html.ValidationMessageFor(model => model.UserName) %>
    20         </div>
    21         <div class="editor-label">
    22             <%: Html.LabelFor(model => model.Password) %>
    23         </div>
    24         <div class="editor-field">
    25             <%: Html.TextBoxFor(model => model.Password) %>
    26             <%: Html.ValidationMessageFor(model => model.Password) %>
    27         </div>
    28         <div class="editor-label">
    29             <%: Html.LabelFor(model => model.Email) %>
    30         </div>
    31         <div class="editor-field">
    32             <%: Html.TextBoxFor(model => model.Email) %>
    33             <%: Html.ValidationMessageFor(model => model.Email) %>
    34         </div>
    35         <div class="editor-label">
    36             <%: Html.LabelFor(model => model.Personal.Age) %>
    37         </div>
    38         <div class="editor-field">
    39             <%: Html.TextBoxFor(model => model.Personal.Age)%>
    40             <%: Html.ValidationMessageFor(model => model.Personal.Age)%>
    41         </div>
    42         <div class="editor-label">
    43             <%: Html.LabelFor(model => model.Personal.Gender) %>
    44         </div>
    45         <%--这里是定义了一个变量用来存取下拉列表里面的选择项gender--%>
    46         <% var gender = new List<SelectListItem>{
    47                new SelectListItem{Text="请选择",Value="-1",Selected=true},
    48                new SelectListItem {Text="",Value="0"},
    49                new SelectListItem{Text="",Value="1"}};%>
    50         <div class="editor-field">
    51             <%: Html.DropDownListFor(model => model.Personal.Gender,gender)%>
    52             <%: Html.ValidationMessageFor(model => model.Personal.Age)%>
    53         </div>
    54         <div class="editor-label">
    55             <%: Html.LabelFor(model => model.Personal.RealName) %>
    56         </div>
    57         <div class="editor-field">
    58             <%: Html.TextBoxFor(model => model.Personal.RealName)%>
    59             <%: Html.ValidationMessageFor(model => model.Personal.RealName)%>
    60         </div>
    61         <p>
    62             <input type="submit" value="Create" />
    63         </p>
    64     </fieldset>
    65     <% } %>
    66     <div>
    67         <%: Html.ActionLink("Back to List", "Index") %>
    68     </div>
    69 </body>
    70 </html>

    我们先看看代码,首先我们先来创建user类,设置器属性。其次我们在创建controller。再次我们创建view,我们使用vs2010工具直接创建强类型视图,有利于我们代码编写效率。

    方法一:给controller中的方法参数添加特性
      [AcceptVerbs(HttpVerbs.Post)]
            public ActionResult Index([Bind(Include = "Personal,Password")]User user)
            {
                return View();
            }

    说明:在其方法中我们给他的方法参数设置bind特性,其中Include这个意思是包含。就是说只绑定Personal,Password属性,其他都不绑定。

    方法二:给Model中的类添加特性

    //[Bind(Include = "Personal,Password")]//自定义绑定限制,这个是类的方法绑定限制
        public class User
        {

            public string UserName { get; set; }

            public string Password { get; set; }

            public string Email { get; set; }

            public Personal Personal { get; set; }
        }

    这个执行效果和上面的一样。

    最后留给大家一个思考:如果我们不绑定Personal,Password这个属性呢?这里提示下:“Exclude”!

    大家自己做测试吧。

  • 相关阅读:
    静态方法、类方法和属性方法
    类的私有属性和私有方法
    JMeter-正则表达式(取出银行卡号后4位)
    JMeter连接MySQL数据库
    解决chrome提示您的连接不是私密连接的方法
    python安装appium模块
    mac中的word内容丢失
    有些事一旦开始就停不下来了
    Python接口测试-以&连接拼接字典数据(get中url请求数据)
    Python接口测试-模块引用与映射
  • 原文地址:https://www.cnblogs.com/ypfnet/p/2686844.html
Copyright © 2011-2022 走看看