In the site development we often need to use the form, then, in the foreground page of the form submitted to the background controller, the background controller how to receive the data submitted by the form? Here we introduce several common methods.
Let's take a look at the front page, where we use a user name and password form as the front page.
First, we create a new MVC project, then add a controller, Userinfocontroller; In the default method index of the controller, we add a view. This index view is used to display our foreground registration page.
The view is as follows: even a simple form ~
The code below, the key point of the view is the method that submits the contents of the form to which controller. This is done through the URL of the action.
@{Layout = null;}<!DOCTYPE HTML><HTML><Head> <Metaname= "Viewport"content= "Width=device-width" /> <title>Index</title></Head><Body> <Div> <!--submitted to the GetUserInfo method in the background controller - <formAction= "~/userinfo/getuserinfo"Method= "POST"> <Table> <TR> <!--each field must be given a unique name, and the background controller uses name to identify - <TD>User name:<inputtype= "text"name= "username" /> </TD> </TR> <TR> <TD>Password:<inputtype= "text"name= "Password" /> </TD> </TR> <TR> <TD> <inputtype= "Submit"value= "Submit" /> </TD> </TR> </Table> </form> </Div></Body></HTML>
Next we need to process the information submitted by the form in the background controller. We first write a method under the UserInfo controller to receive the data that the table only son over.
The first method, the key point is that the parameter names must be consistent with the name of the form.
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSYSTEM.WEB.MVC;namespacemvcdemo.controllers{ Public classUserinfocontroller:controller {//Get:userinfo PublicActionResult Index () {returnView (); } //the name of the parameter needs to be the same as the field name of the form, so the system assigns the value directly. PublicActionResult GetUserInfo (stringUsernamestringpassword) { //to facilitate the demonstration, we directly output these two values, indicating that we have obtained the data returnContent (username+"*****"+password); } }}
The second method, FormCollection contains all the values of the form, is actually a key-value pair, and the key is the name in the form field
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSYSTEM.WEB.MVC;namespacemvcdemo.controllers{ Public classUserinfocontroller:controller {//Get:userinfo PublicActionResult Index () {returnView (); } //formcollection contains all the values of a form, which is actually a key-value pair, and the key is the name in the form field. Publicactionresult GetUserInfo (formcollection collection) {stringUsername = collection["username"]; stringPassword = collection["Password"]; //to facilitate the demonstration, we directly output these two values, indicating that we have obtained the data returnContent (username+"*****"+password); } }}
The third method is to take the value directly.
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSYSTEM.WEB.MVC;namespacemvcdemo.controllers{ Public classUserinfocontroller:controller {//Get:userinfo PublicActionResult Index () {returnView (); } PublicActionResult GetUserInfo () {stringUsername = request["username"]; stringPassword = request["Password"]; //to facilitate the demonstration, we directly output these two values, indicating that we have obtained the data returnContent (username+"*****"+password); } }}
The fourth type is to accept the field information by creating an object. As long as the object's properties and name correspond, the system is automatically assigned a value.
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSYSTEM.WEB.MVC;namespacemvcdemo.controllers{ Public classUserinfocontroller:controller {//Get:userinfo PublicActionResult Index () {returnView (); } Publicactionresult getuserinfo (user user) {stringUsername =user. Username; stringPassword =user. Password; //to facilitate the demonstration, we directly output these two values, indicating that we have obtained the data returnContent (username+"*****"+password); } } Public classUser {Private stringusername; Public stringUsername {Get{returnusername;} Set{username =value;} } Private stringpassword; Public stringPassword {Get{returnpassword;} Set{Password =value;} } }}
Asp. net method of acquiring form form values in MVC controller