[Asp.net mvc] transmits form data in the view to the Controller. asp. netmvc

Source: Internet
Author: User

[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.

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.