In the ASP. net mvc Framework, the data in the view is transmitted to the Controller, which is implemented by sending a form. In actual use, the following three methods are used.
1. Read form data through request. Form 2. Read form data through formcollection 3. Directly read form Data Objects
Below is a summary of these things.
1. Read form data through request. Form
First, define a person class as follows:
Public ClassPerson {Public StringFirstname {Get;Set;}Public StringLastname {Get;Set;}} Define the following method in homecontroller to receive data from the View:
[Acceptverbs (httpverbs. Post)]PublicActionresult requestform () {person =NewPerson (); person. firstname = request. Form ["Firstname"]; Person. lastname = request. Form ["Lastname"];ReturnView (person );}
This method reads data from the two text boxes "firstname" and "lastname" sent from the view through request. Form, and obtains the person class instantiated object person.
The data is sent from homeview. The form is implemented.CodeAs follows:
<Fieldset> <p> <%Using(Html. beginform ("Requestform","Home") {%> Firstname: <% = html. Textbox ("Firstname") %> <Br/> lastname: <% = html. Textbox ("Lastname") %> <Input type ="Submit"Name ="Submit"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 of the receiving form, and the second parameter indicates the controller of the receiving form.
This statement specifies that action requestform in the home controller accepts the data just passed. The sent form data has two text boxes: "firstname" and "lastname"
The result of clicking submit is as follows:
As you can see, the object person successfully receives data from the form.
2. Read form data through formcollection
The formcollection object in ASP. net mvc is the set of all objects in the submitted form.
To read form data through formcollection, set the following form:
<Fieldset>
<P>
<% Using (html. beginform ("formcollection", "home "))
{%>
Firstname: <% = html. Textbox ("firstname") %> <br/>
Lastname: <% = html. Textbox ("lastname") %> <br/>
<Input type = "Submit" name = "Submit" value = "formcollection"/> <br/>
<% }%>
</P>
</Fieldset>
(The insert code block function of writer suddenly becomes useless .....)
It can be seen that the action for processing the form is the formcollection method in homecontroller. The form sent is still the text box for reform in the coming year.
The formcollection method is implemented as follows:
[Acceptverbs (httpverbs. Post)]
Public actionresult formcollection (formcollection)
{
Person = new person ();
Person. firstname = formcollection ["firstname"];
Person. lastname = formcollection ["lastname"];
Return view (person );
}
The formcollection type is provided to pass in form values.Program(This is not quite understandable in msdn). The formcollection () method passes in a formcollection type parameter, which will automatically bind all data in the form.
You can use formcollection to obtain the data in two text boxes, and then get the instantiated object person of the person class. The result is as follows:
3. Directly read form Data Objects
To directly read form objects, 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 for processing this form is the person method in homecontroller. Send 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 above Code, the person () method has passed in the person type parameter, which reads data in the "firstname" and "lastname" text boxes internally, directly obtain the instantiated object person of the person class.
The program running result is as follows:
When reading a table object directly, the text box sent to the form must be the same as the name of the Data Object attribute (Case Insensitive)
Technorati label: MVC, form data binding