[ASP. NET MVC] passes the form data in the view to the controller

Source: Internet
Author: User

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

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.