In the ASP. NET MVC framework, the data in the view is passed to the controller, which is implemented primarily by sending the form. In particular, the following three methods are used mainly.
1. Reading form data via Request.Form
Form code:
1 @using (Html.BeginForm ("person", "DEFAULT3"))2 {3 @Html. TextBox ("Tfirstname")4 <BR/>5 @Html. TextBox ("Tlastname")6 <BR/>7 <inputtype= "Submit"value= "Submit" /> 8}
Extract form data using Request.Form:
1 [HttpPost]2 Publicjsonresult person ()3 {4Jsonresult result =NewJsonresult ();5Result. Data =New Object[] {request.form["FirstName"], request.form["LastName"] };6 returnresult;7}
2. Reading form data via FormCollection
1 [HttpPost]2 Publicjsonresult person (formcollection form)3 {4Jsonresult result =NewJsonresult ();5Result. Data =New Object[] {form["FirstName"], form["LastName"] };6 returnresult;7}
3. Direct reading of form data Objects
First define a person class as follows:
1 public class Person2 {3 public string FirstName {get; set;} 4 public string LastName {get; set;} 5 }
Implementation of person ():
1 [HttpPost]2 Public jsonresult person ( person person) 3 {4 new jsonresult (); 5 New Object [] {person. FirstName, person. LastName}; 6 return result; 7 }
The field name of the person class must match the name of input in the form.
Or:
1 [HttpPost] 2 public jsonresult person (string FirstName, string LastName) 3 { 4 Jsonresult result = new Jsonresult (); 5 result. Data = new object [] {FirstName, LastName}; 6 return result; 7 }
The parameter name must match the name of input in the form.
[ASP. NET MVC] passes the form data in the view to the controller