The post, get mode correspondence of the view chart single access Controller operation method

Source: Internet
Author: User

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

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.