asp.net mvc中的模型綁定可以在提交http請求的時候,進行數據的映射。
1.沒有模型綁定的時候
1 public ActionResult Example0() 2 { 3 if (Request.Form.Count > 0) 4 { 5 string id = Request.Form["Id"]; 6 string fname =Request.Form["FirstName"]; 7 string lname = Request.Form["LastName"]; 8 ViewBag.StatusMessage = "Employee data received successfully for ID " + id + "!"; 9 } 10 return View(); 11 }
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 }