In the view, the form default access is FormMethod.Post (the request is not displayed in the Address bar). In the controller, the action method does not label the property, and the default is the HttpGet property. There will be a previous situation.
1, the form does not specify access mode (the default is post), there is only one action method, and does not label the property, the default is the HttpGet property. The form submits the data to the controller HttpGet method.
@using (Html.BeginForm ())
{
<div class= "Form-group" >
<label for= "searchstring" class= "Control-label" > Name:</label>
</div>
<div class= "Form-group" >
@Html. TextBox ("SearchString", "", htmlattributes:new {@class = "Form-control", placeholder = "Please enter the name of the Tablet"})
</div>
<input type= "Submit" value= "Find" class= "btn btn-primary"/>
}
2, the form does not specify access mode (the default is post), the controller action method has two overloads, respectively, [HttpGet] and [HttpPost] properties. The form submits the data to the controller HttpPost method.
3. The form specifies that the data is sent as Formmethod.get, which enables the post request to be routed to the [HttpGet] version (the default), and the requested address can be displayed in the URL address bar. The form in view is as follows: @using (Html.BeginForm ("Index", "Movies", Formmethod.get))
@using (Html.BeginForm ("Index", "Movie", Formmethod.get, htmlattributes:new {@class = "form-inline", role = "form"}))
{
<div class= "Form-group" >
<label for= "searchstring" class= "Control-label" > Name:</label>
</div>
<div class= "Form-group" >
@Html. TextBox ("SearchString", "", htmlattributes:new {@class = "Form-control", placeholder = "Please enter the name of the Tablet"})
</div>
<input type= "Submit" value= "Find" class= "btn btn-primary"/>
}
In summary, the operation method of the controller [HttpGet] property can accept form values sent as GET, post. The action method of the controller [HttpPost] property can only accept forms in post form.
The Get form can only send the [HttpGet] property in an action method; The Post form sends the data to the action method of the [HttpGet], [HttpPost] property, but gives precedence to the action method of the [HttpPost] property.
Generally speaking, 1, if the operation method is generally not change the state of the program, that is, do not increase the deletion (query only) the operation of the database. Use the Formmethod.get form as (@using (Html.BeginForm ("Index", "Movies", Formmethod.get)), [HttpGet] property (the default style, Do not set) the action method. 2, if it involves the deletion of the database, change operation, using the Post form, the default style, form such as @using (HTML. BeginForm ()), the action method of the [HttpPost] property.
The post, get mode correspondence of the view chart single access Controller operation method