Model bindings in ASP.

Source: Internet
Author: User

The ASP. NET MVC Model binding simplifies controller operations by introducing an abstraction layer that automatically populates the controller action parameters, and handling common attribute mappings and type conversion code that are typically related to using ASP. Although model binding may seem simple, it is actually a relatively complex framework that consists of many parts that collectively create and populate the objects required to manipulate the controller.

Model bindings in ASP. NET MVC can map data when an HTTP request is submitted.

1. When there is no model binding

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  } return View (); 11}

View Code

2. Simple Binding Data

1 [HttpPost] 2 public actionresult Example1 (string ID, string firstname, String lastname) 3 {4 Viewbag.statusmessage = "E Mployee data received successfully for ID "+ ID +"! "; 5 return View (); 6}

Page content

1 <tr>  2  ... 3 <td>  4 <input name= "Id" type= "text"/>  5 </td>  6 </tr>  7 <tr>  8 ...  9 <td> <input name= "FirstName" type= "text"/> </td> </tr> 14 ... <td> <input name= "LastName" type= "text"/> </td> </tr>

3. Binding a class type

1 [HttpPost] 2 public actionresult Example2 (employee EMP) 3 {4 Viewbag.statusmessage = "Employee Data received successful Ly for ID "+ EMP. Id + "!"; 5 return View (); 6}


Classes are as follows:

1 public class Employee 2 {3 publicly string Id {get; set;} 4 public string FirstName {get; set;} 5 public string lastn Ame {get; set;} 6}

4. Binding a Class Property

1 [HttpPost] 2 public actionresult Example3 (employee EMP) 3 {4 Viewbag.statusmessage = "Employee Data received successful Ly for ID "+ EMP. Id + "!"; 5 return View (); 6}

Classes are as follows:

1 public class Employee 2 {3 publicly string Id {get; set;} 4 public string FirstName {get; set;} 5 public string lastn Ame {get; set;} 6 public Address homeaddress {get; set;} 7}
1 public class Address 2 {3 publicly string Street {get; set;} 4 public string country {get; set;} 5 public string Post Alcode {get; set;} 6}


Page content:

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 ... One <td> <input name= "Homeaddress.postalcode" type= "text"/></td> </tr>


5. Binding a collection of simple types

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}

Page content:

  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> <td align= "right" nowrap= "nowrap" width= "15%" > <input name= "id" type= "text" size= "/&G" T </td> <td> <input name= "name" type= "text"/> </td> + </tr> + <tr> <  TD align= "Right" nowrap= "nowrap" width= "15%" > <input name= "id" type= "text"/> </td> + <td> 22 <input name= "name" type= "text"/> </td> </tr> 25 ...


6. Binding a collection of classes

It classic Laugh quote: system programmer: 1, scalp often numbness, in the sight of a blue screen is more obvious, in the screen is not visible when the time is particularly obvious; 2, when the elevator is always worried about the crash, and the wall to find the reset key; 3, the nail is very long, Because according to F7 to F12 relatively labor-saving, 4, as long as there is something in the hand, and kept pressing, thought is alt-f, s;5, the chassis never on the lid, in order to determine whether the hard disk is turning; 6, often inexplicably tracking others, hands kept pressing F10;7, all the interfaces are plugged in the hard disk, Therefore, 26 letters are not enough, 8, a free to say "next life does not do programmers", 9, always think 9th is a number, 10, not afraid of the virus, but very afraid of their own procedures;

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}

Page content:

  1 ... 2 <tr>  3 <td align= "right" nowrap= "nowrap" width= "15%" >  4 <input name= "[0].id" type= "text" size= "/>"  5 </td>  6 <td>  7 <input name= "[0]. FirstName "type=" text "/>  8 </td>  9 <td> <input name=" [0]. LastName "type=" text "/> </td> </tr> <tr> <td align=" right "nowrap=" nowrap "width=" 15% "> <input name=" [1].id "type=" text "size=" "/> [</td>] <td> <input name=" [1]. FirstName "type=" text "/> </td> <td> <input name=" [1]. LastName "type=" text "/> </td> </tr> 24 25 ...

Note that the index starts at 0, and the middle is uninterrupted.

If you encounter dynamic add and delete functions, then it is not easy to set up, you can use the following method:

Adds a hidden control with the name suffix of the control. Index

Controller unchanged, the page content is changed to:

  1 ... 2 <tr>  3 <td align= "right" nowrap= "nowrap" width= "15%" >  4 <input  type= "hidden" name= " Employees. Index "value="/>  5 <input name= employees[100].id "type=" text "size="/>  6 </td>  7 & Lt;td>  8 <input name= "employees[100]. FirstName "type=" text "/>  9 </td> <td> <input name=" employees[100 ". LastName "type=" text "/> </td> </tr>" <tr> <td align= "right" nowrap= "nowrap" width= "15% "> <input  type=" hidden "name=" employees.  Index "value=" CCC "/> <input name=" employees[ccc].id "type=" text "size="/> "</td> <td> 20 <input name= "EMPLOYEES[CCC]. FirstName "type=" text "/> </td> <td> <input name=" EMPLOYEES[CCC]. LastName "type=" text "/> </td> </tr> 26 ...


7. Customizable Models

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}

Binding method:

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"), ten int. Parse (controllercontext.httpcontext.request.form["Month"]), one int. Parse (controllercontext.httpcontext.request.form["Day")); return EMP;  14}
Http://www.codes51.com/article/detail_95556.html

Model binding in ASP. NET MVC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.