asp.net mvc中的模型绑定可以在提交http请求的时候,进行数据的映射。
1.没有模型绑定的时候
View Code
2.简单绑定数据
1 [HttpPost] 2 public ActionResult Example1(string id, string firstname, string lastname) 3 { 4 ViewBag.StatusMessage = "Employee data received successfully for ID " + id + "!"; 5 return View(); 6 }
页面内容
1 <tr> 2 ... 3 <td> 4 <input name="Id" type="text" /> 5 </td> 6 </tr> 7 <tr> 8 ... 9 <td> 10 <input name="FirstName" type="text" /> 11 </td> 12 </tr> 13 <tr> 14 ... 15 <td> 16 <input name="LastName" type="text" /> 17 </td> 18 </tr>
3.绑定一个类类型
1 [HttpPost] 2 public ActionResult Example2(Employee emp) 3 { 4 ViewBag.StatusMessage = "Employee data received successfully for ID " + emp.Id + "!"; 5 return View(); 6 }
类如下:
1 public class Employee 2 { 3 public string Id { get; set; } 4 public string FirstName { get; set; } 5 public string LastName { get; set; } 6 }
4.绑定一个类 属性
1 [HttpPost] 2 public ActionResult Example3(Employee emp) 3 { 4 ViewBag.StatusMessage = "Employee data received successfully for ID " + emp.Id + "!"; 5 return View(); 6 }
类如下:
1 public class Employee 2 { 3 public string Id { get; set; } 4 public string FirstName { get; set; } 5 public string LastName { get; set; } 6 public Address HomeAddress { get; set; } 7 }
1 public class Address 2 { 3 public string Street { get; set; } 4 public string Country { get; set; } 5 public string PostalCode { get; set; } 6 }
页面内容:
1 <tr> 2 ... 3 <td> 4 <input name="HomeAddress.Street" type="text" /></td> 5 </tr> 6 ... 7 <td> 8 <input name="HomeAddress.Country" type="text" /></td> 9 </tr> 10 ... 11 <td> 12 <input name="HomeAddress.PostalCode" type="text" /></td> 13 </tr>
5.绑定简单类型的集合
1 [HttpPost] 2 public ActionResult Example4(IList<string> id, IList<string> name) 3 { 4 ViewBag.StatusMessage = "Employee data received successfully for " + id.Count + " records!"; 5 return View(); 6 }
页面内容:
1 ... 2 <tr> 3 <td align="right" nowrap="nowrap" width="15%"> 4 <input name="id" type="text" size="20" /></td> 5 <td> 6 <input name="name" type="text" /> 7 </td> 8 </tr> 9 <tr> 10 <td align="right" nowrap="nowrap" width="15%"> 11 <input name="id" type="text" size="20" /> 12 </td> 13 <td> 14 <input name="name" type="text" /> 15 </td> 16 </tr> 17 <tr> 18 <td align="right" nowrap="nowrap" width="15%"> 19 <input name="id" type="text" /> 20 </td> 21 <td> 22 <input name="name" type="text" /> 23 </td> 24 </tr> 25 ...
6.绑定一个类的集合
1 [HttpPost] 2 public ActionResult Example5(IList<Employee> employees) 3 { 4 ViewBag.StatusMessage = "Employee data received successfully for " + employees.Count + " records!"; 5 return View(); 6 }
页面内容:
1 ... 2 <tr> 3 <td align="right" nowrap="nowrap" width="15%"> 4 <input name="[0].id" type="text" size="20" /> 5 </td> 6 <td> 7 <input name="[0].FirstName" type="text" /> 8 </td> 9 <td> 10 <input name="[0].LastName" type="text" /> 11 </td> 12 </tr> 13 <tr> 14 <td align="right" nowrap="nowrap" width="15%"> 15 <input name="[1].id" type="text" size="20" /> 16 </td> 17 <td> 18 <input name="[1].FirstName" type="text" /> 19 </td> 20 <td> 21 <input name="[1].LastName" type="text" /> 22 </td> 23 </tr> 24 25 ...
注意索引是从0开始,中间不间断
如果,遇到有动态的Add和Delete功能,则所以不好去设置,可以使用下面的方法:
添加一个隐藏控件,控件名称后缀为.Index
Controller不变,页面内容更改为:
1 ... 2 <tr> 3 <td align="right" nowrap="nowrap" width="15%"> 4 <input type="hidden" name="employees.Index" value="100" /> 5 <input name="employees[100].id" type="text" size="20" /> 6 </td> 7 <td> 8 <input name="employees[100].FirstName" type="text" /> 9 </td> 10 <td> 11 <input name="employees[100].LastName" type="text" /> 12 </td> 13 </tr> 14 <tr> 15 <td align="right" nowrap="nowrap" width="15%"> 16 <input type="hidden" name="employees.Index" value="ccc" /> 17 <input name="employees[ccc].id" type="text" size="20" /> 18 </td> 19 <td> 20 <input name="employees[ccc].FirstName" type="text" /> 21 </td> 22 <td> 23 <input name="employees[ccc].LastName" type="text" /> 24 </td> 25 </tr> 26 ...
7.可自定义模型
1 [HttpPost] 2 public ActionResult Example6([ModelBinder(typeof(EmployeeBinder1))]Employee employee) 3 { 4 ViewBag.StatusMessage = "Employee data received successfully for " + employee.Id + "!"; 5 return View(); 6 }
绑定方法:
1 public class EmployeeBinder1:IModelBinder 2 { 3 public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 4 { 5 Employee emp = new Employee(); 6 emp.Id = "E" + controllerContext.HttpContext.Request.Form["Id"]; 7 emp.FirstName = controllerContext.HttpContext.Request.Form["FirstName"]; 8 emp.LastName = controllerContext.HttpContext.Request.Form["LastName"]; 9 emp.BirthDate = new DateTime(int.Parse(controllerContext.HttpContext.Request.Form["year"]), 10 int.Parse(controllerContext.HttpContext.Request.Form["month"]), 11 int.Parse(controllerContext.HttpContext.Request.Form["day"])); 12 return emp; 13 } 14 }