MVC模型绑定

在项目中使用MVC的时候,我们无需像WebForm那样手动获取值再赋值到Model上,这得益于MVC的模型绑定,下面就介绍下复杂类型的模型绑定

Controller:

 1     public class HomeController : Controller
 2     {
 3         [HttpGet]
 4         public ActionResult Index()
 5         {
 6             return View();
 7         }
 8 
 9         [HttpPost]
10         public ActionResult Index(Person person)
11         {
12             return Json(person);
13         }
14     }

Model:

 1     public class Person
 2     {
 3         public int Id { get; set; }
 4         public string Name { get; set; }
 5         public Address Address { get; set; }
 6         public List<Role> Roles { get; set; }
 7     }
 8 
 9     public class Address
10     {
11         public string Country { get; set; }
12         public string City { get; set; }
13     }
14 
15     public class Role
16     {
17         public string Name { get; set; }
18     }

View:

 1 @{
 2     Layout = null;
 3 }
 4 
 5 <!DOCTYPE html>
 6 
 7 <html>
 8 <head>
 9     <meta name="viewport" content="width=device-width" />
10     <title>Index</title>
11     <script src="~/Scripts/jquery-1.8.2.min.js"></script>
12     <script type="text/javascript">
13         //如果没有复杂类型,也可以使用这种
14         //var jsonData = {
15         //    Id: 1,
16         //    Name: "李四"
17         //};
18         //$.post("/home/index", jsonData, function (data) {
19         //    console.log(data);
20         //});
21         var paramsUrl = "Id=1&Name=李四&Address.City=广州&Address.Country=中国&Roles[0].Name=管理员&Roles[1].Name=科长&Roles[2].Name=馆长";
22         $.post("/home/index", paramsUrl, function (data) {
23             console.log(data);
24         });
25     </script>
26 </head>
27 <body>
28     <div>
29 
30     </div>
31 </body>
32 </html>

发送请求 post参数:

返回:

在MVC5(5.2.3.0)之前我们需要拼接成这种参数后台Index(Person person)才能接收到对应值,但是MVC5(5.2.3.0)版本的时候可以直接传递Json对象

升级为MVC5.2.3.0后,只需改视图即可:

 1 @{
 2     Layout = null;
 3 }
 4 
 5 <!DOCTYPE html>
 6 
 7 <html>
 8 <head>
 9     <meta name="viewport" content="width=device-width" />
10     <title>Index</title>
11     <script src="~/Scripts/jquery-1.8.2.min.js"></script>
12     <script type="text/javascript">
13         var jsonData = {
14             Id: 1,
15             Name: "李四",
16             Address: { City: "广州", Country: "中国" },
17             Roles: [{ Name: "管理员" }, { Name: "科长" }, { Name: "馆长" }]
18         };
19         $.post("/home/index", jsonData, function (data) {
20             console.log(data);
21         });
22     </script>
23 </head>
24 <body>
25     <div>
26 
27     </div>
28 </body>
29 </html>

发送请求post参数,这里会将url转码:

解码后【MVC5(5.2.3.0)之前不能接收 Address[City]=广州 这种方式的传值】:

返回(跟之前返回一样):

另外还可以使用Bind特性来决定需要绑定哪些属性

1         [HttpPost]
2         public ActionResult Index([Bind(Include = "Name,Age")]Person per)
3         {
4             return Json(per);
5         }

[Bind(Include = "Name,Age")] 表示只绑定Person对象中的Name和Age属性,其他属性则为默认值

[Bind(Exclude = "Name,Age")] 这个表示不绑定Person对象中的Name和Age属性

也可以直接在Model加上BindAttribute来决定绑定和不绑定哪些属性

1     [Bind(Include="Name,Age")]
2     public class Person
3     {
4         public int Id { get; set; }
5         public string Name { get; set; }
6         public int Age { get; set; }
7     }

Bind还有一个参数是Prefix,这个是用来指定前缀,具体看下面例子

Model:

1     public class Person
2     {
3         public int Id { get; set; }
4         public string Name { get; set; }
5         public int Age { get; set; }
6         public Address Address1 { get; set; }
7     }

View:

1         <form action="/home/index" method="post">
2             @Html.TextBoxFor(p => p.Address1.City)
3             @Html.TextBoxFor(p => p.Address1.Country)
4             <input type="submit" value="提交" />
5         </form>

Action:

1         [HttpPost]
2         public ActionResult Index(Address address)
3         {
4             return Json(address);
5         }

提交的内容:

返回:

如果在不使用Prefix的情况下我们是获取不到提交的值的,使用Prefix:

1         [HttpPost]
2         public ActionResult Index([Bind(Prefix = "Address1")]Address address)
3         {
4             return Json(address);
5         }

加上[Bind(Prefix = "Address1")]:

灵活运用将会在开发过程中如鱼得水,哈哈

原文地址:https://www.cnblogs.com/zuqing/p/5728601.html