Three ways to pass data from view to a controller in ASP. NET MVC (form data binding)

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 forms. In use, the following three methods are used mainly.

1. Read form data by Request.Form 2, read form data by FormCollection 3, directly read form data object

Here's a summary of what I learned from these things.

1. Read form data via Request.Form

first define a person class as follows:

    Class person    {        set;}} The following method is defined in HomeController to receive data from view:
        [Acceptverbs (Httpverbs.post)]        Public ActionResult RequestForm ()        {            new person ();            Person. FirstName = request.form["FirstName"];            Person. LastName = request.form["LastName"];            return View (person);            
This method reads the data in the two text boxes "FirstName" and "LastName" from view by Request.Form, and then gets the person class instantiation object person.
The data came from Homeview. The code to implement the form is as follows:
    <fieldset>        <p>            <%using (Html.BeginForm ("requestform", "Home")              {%>            Firstname:<%=html.textbox ("FirstName")%><br/>            lastname:<%=html.textbox ("  LastName ")%>            <input type="submit "Name="value= "requestform"/><br/> <%}%> </p> </fieldset>       
Where: Html.BeginForm ("RequestForm", "Home") is an extension method in the FormExtensions class in MVC. The first parameter indicates the action to receive the form, and the second parameter indicates the controller that accepted the form.
This statement indicates that the action RequestForm in the home controller accepts the data just passed in. Sent form data two text boxes: "FirstName" and "LastName"
Click the Submit button to submit the results as follows:

You can see that the object person successfully received the data from the form.

2. Read form data via FormCollection

The FormCollection object in ASP. NET MVC is a collection of all the objects in the submitted form.

In order to read the form data through FormCollection, set up the following form:

<fieldset> <p> <%using (Html.BeginForm ("FormCollection", "Home") {%> Fir         Stname:<%=html.textbox ("FirstName")%><br/> Lastname:<%=html.textbox ("LastName")%><br/> <input type= "Submit" name= "Submit" value= "FormCollection"/><br/> <%}%> </p> </fieldset>

(Writer's insert code block function suddenly can not be used, depressed ...)
As you can see, the action that processes the form is the FormCollection method in HomeController. The sent form remains the reform text box for the following year.
The FormCollection method is implemented as follows:

[Acceptverbs (Httpverbs.post)] public actionresult formcollection (formcollection formcollection) {            person person = new person (); Person.            FirstName = formcollection["FirstName"]; Person.            LastName = formcollection["LastName"];        return View (person); }

The formcollection type is the provider that passes in the form's values in the form (this is not understood in MSDN). A parameter of type formcollection is passed in the FormCollection () method, which automatically binds all the data in the form.

You can get the data in two text boxes by FormCollection, and then get the instantiated object person for the person class. The results are as follows:

 

3. Direct reading of form data Objects

To read the form object directly, set the following form:

<fieldset> <p> <%using (html.beginform ("person", "Home") {%> Firstname:<%=html.textbox ("FirstName")%><br/> Lastname:<%=html.textbox ("LastName")%><br /> <input type= "Submit" name= "submit" value= "person"/><br/> <%}%> < ;/p> </fieldset>

The action that handles the form is the person method in HomeController. Sends the data in two text boxes.

The implementation of person () is as follows:

public class Person {public string FirstName {get; set;} public string LastName {get; set;}}

In the code above, the person type parameter is passed in the person () method, which internally reads the data from "FirstName" and "LastName" in two text boxes, and gets the instantiated object person of the person class directly. The running results of the program are as follows:

When reading a table object directly, the text box that sends the form must match the name of the data object property (case insensitive)

Three ways to pass data from view to a controller in ASP. NET MVC (form data binding)

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.