Add an action to the Homecontrol with the following code:
Public ActionResult Add () { return View ();}
When no parameters are written in view (), the default is to call a view with the same name, add a view add.cshtml, and add a form with the following code:
<Body> <Div>@using (Html.BeginForm ()) {<inputtype= "text"ID= "Id1"name= "Id1"/><BR/> <inputtype= "text"ID= "Name1"name= "Name1"/><BR/> <inputtype= "text"ID= "Gender"name= "Gender"/><BR/> <inputtype= "Submit"value= "Submit"/> } </Div></Body>
From the browser, you can see that a form has been generated, such as:
<formAction= "/home/add"Method= "POST"> <inputtype= "text"ID= "Id1"name= "Id1"/><BR/> <inputtype= "text"ID= "Name1"name= "Name1"/><BR/> <inputtype= "text"ID= "Gender"name= "Gender"/><BR/> <inputtype= "Submit"value= "Submit"/> </form>
You can see that there are no write parameters in BeginForm (), which are submitted by default to the current background, POST request. It's like ASP.
Now that you have submitted to the background, then the background must have a way to accept the data, and the same name as the requested view, then we will create an action, the code is as follows:
[HttpPost] Publicactionresult Add (UserInfo user) {intA =Ten;//breakpoints are set here if(User. Id = =0) { intID =int. Parse (request["Id1"]); stringName = request["Name1"]; stringgender = request["Gender"]; User=NewUserInfo () {id = id, name = name, Gender =Gender}; } returnContent ("OK"); }
The action accepts a POST request and is overloaded with the original add (), with parameters of type UserInfo, and the code for the UserInfo class is as follows:
namespace _02htmlhelper.models{ publicclass UserInfo { public int Get Set ; } Public string Get Set ; } Public string Get Set ; } }}
in int a=10, set a breakpoint in front and choose tools-Add to process-iisexpress managed
Then in the HTML page refresh, and then enter the data in the HTML, submit the form, this time, will automatically run to the breakpoint, the test input data such as:
At this point, you can see the parameters of the incoming add () method, and the values in user are as follows:
Analyze the results and draw knowledge:
The incoming values are: Id1=11;name1=22;gender=33 The UserInfo method receives a parameter of Id=0; Name=null; Gender=33
1. Case of name in client is not important 2. Regardless of the value of the passed-in ID, the ID always has a value, which is important 3. When the current station sends the data to the background, MVC will automatically stitch the parameters into objects, so we can write a lot less code when we use the object in the foreground. |
Next modify the foreground code, in the way of the object, because the view at the time of adding, for the strong type of some type, so in the beginning of the strong type of userinfo, the code is as follows:
@model _02htmlhelper.models.userinfo@{Layout = null;}<!DOCTYPE HTML><HTML><Head> <Metaname= "Viewport"content= "Width=device-width" /> <title>Add</title></Head><Body> <Div>@using (Html.BeginForm ()) {@Html. Textboxfor (u=>u.id)<BR/>@Html. Textboxfor (u=>u.name)<BR/>@Html. Textboxfor (U=>u.gender)<BR/> <inputtype= "Submit"value= "Submit"/> } </Div></Body></HTML>
After running the above page, look at the source code, as below, for name= ' Id ' section, will be described in another article:
<formAction= "/home/add"Method= "POST"><inputData-val= "true"Data-val-number= "Field Id must be a number." "data-val-required= "Id field is required. "ID= "Id"name= "Id"type= "text"value="" /><BR/> <inputID= "Name"name= "Name"type= "text"value="" /><BR/> <inputID= "Gender"name= "Gender"type= "text"value="" /><BR/> <inputtype= "Submit"value= "Submit"/> </form>
Follow the above steps, set a breakpoint at int a=10, add a process, run, view the value of user in the incoming add (UserInfo user), all the values, the background is all accepted.
MVC Learning-Sending requests