asp.net Mvc 的自定义模型绑定

本文详细介绍了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”!

大家自己做测试吧。

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值