One: Simple model binding
In ASP., the model binding is used to parse the data transmitted by the client, and it is simpler to encapsulate the means of obtaining the data, so that the user can get the data more conveniently.
Let's take a simple example.
Public actionresult Index () { return View (); } [HttpPost] Public ActionResult Index (string username) { viewdata["username "] = username; return View (); }
Here we define a Action,[httppost] qualifying action can only be accessed by a POST request, and we define a string with the name username in its arguments
The code in view view is as follows
@{Viewbag.title="Index";}<form method="Post"> Name:<input type="text"Name="username"Id="username"/> <input type="Submit"Value="Submit"/></form>@if (viewdata["username"] !=NULL){ "username"]}
Note here that the model-bound Name property needs to be the same as the input Label tool in the View view
Two: Model binding using FormCollection
In ASP, we used to use Request.Form to get the value of the submitted form, of course, in MVC we can easily get its value
PublicActionResult Index () {returnView (); } [HttpPost] PublicActionResult Index (formcollection e) {viewdata["username"] = e["username"]; viewdata[" Age"] = e[" Age"]; returnView (); }
In the view, the code is
<form method="Post"> Name:<input type="text"Name="username"Id="username"/>Age:<input type="text"Name=" Age"Id=" Age"/><br/> <input type="Submit"Value="Submit"/></form>@if (viewdata["username"] !=NULL){ <label> name:</label>"username"]" Age"]}
We can clearly see in this way that we can get the data.
Three: Transfer object of Model binding
All we've discussed before is passing some simple values through the form, now if we're going to pass a model object can we? Let's add a people to the model layer first.
Public class people { publicintgetset;} Public string Get Set ; } Public int Get Set ; } }
Modify the code in the controller
PublicActionResult Index () {returnView (); } [HttpPost] PublicActionResult Index (People people) {viewdata["Id"] =people. Id; viewdata["Name"] =people. Name; viewdata[" Age"] =people. Age; returnView (); }
At this point we bind the model to the people type
In view, change to
<form method="Post">Id:<input type="text"Name="Id"Id="Id"/>Name:<input type="text"Name="Name"Id="Name"/>Age:<input type="text"Name=" Age"Id=" Age"/><br/> <input type="Submit"Value="Submit"/></form>@if (viewdata["Id"] !=NULL){ <label>Id:</label>"Id"]"Name"]" Age"]}
Of course here we will define the name property of the input tag as the class people name
In this case, you might think that you can pass multiple objects?
We'll change the controller's code.
PublicActionResult Index () {returnView (); } [HttpPost] PublicActionResult Index (People people1, people people2) {viewdata["Id1"] =People1. Id; viewdata["Name1"] =People1. Name; viewdata["Age1"] =People1. Age; viewdata["Id2"] =People2. Id; viewdata["Name2"] =People2. Name; viewdata["Age2"] =People2. Age; returnView (); }
<form method="Post">Id1:<input type="text"Name="People1. Id"Id="People1. Id"/>Name 1:<input type="text"Name="People1. Name"Id="People1. Name"/>Age 1:<input type="text"Name="People1. Age"Id="People1. Age"/><br/>ID2:<input type="text"Name="People2. Id"Id="People2. Id"/>Name 2:<input type="text"Name="People2. Name"Id="People2. Name"/>Age 2:<input type="text"Name="People2. Age"Id="People2. Age"/><br/> <input type="Submit"Value="Submit"/></form>@if (viewdata["Id1"] !=NULL){ <label>Id1:</label>"Id1"]"Name1"]"Age1"]"Id2"]"Name2"]"Age2"]}
Note: View views We must set the Name property of the label to the same name as the model binding
ASP. NET MVC learning three-data transfer model binding