[Asp.net mvc] transmits form data in the view to the Controller. asp. netmvc
In the ASP. net mvc Framework, the data in the view is transmitted to the Controller, which is implemented by sending forms. The following three methods are used.
1. Read Form data through Request. Form
Form code:
1 @ using (Html. beginForm ("Person", "Default3") 2 {3 @ Html. textBox ("tFirstName") 4 <br/> 5 @ Html. textBox ("tLastName") 6 <br/> 7 <input type = "submit" value = "submit"/> 8}
Use Request. Form to extract Form data:
1 [HttpPost]2 public JsonResult Person()3 {4 JsonResult result = new JsonResult();5 result.Data = new object[] { Request.Form["FirstName"], Request.Form["LastName"] };6 return result;7 }
2. Read form data through FormCollection
1 [HttpPost]2 public JsonResult Person(FormCollection form)3 {4 JsonResult result = new JsonResult();5 result.Data = new object[] { form["FirstName"], form["LastName"] };6 return result;7 }
3. Directly read 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 }
Person () Implementation:
1 [HttpPost]2 public JsonResult Person(Person person)3 {4 JsonResult result = new JsonResult();5 result.Data = new object[] { person.FirstName, person.LastName };6 return result;7 }
The field name of the Person class must be the same as the input name 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 be the same as the input name in the form.