john john - 1 year ago 126
ASP.NET (C#) Question

MVC 5 [HttpPost] attribute or post inside the form tag?

I am new to MVC and I was wondering what's the difference between the attribute [HttpPost] and the post method inside the form

<form action="" method="post">
<input type="text" name="something" value="something" />
<input type="text" name="something" value="something"/>
<input type="submit" value="Submit" />

? Are they the same thing or... I am confused

Answer Source
<form action="" method="post">  </form>

Here the method="post" is not an ASP.NET MVC thing. It is normal HTML form tag specification where it tells that when the form is submitted, it will use the POST method (rather than GET where form data is submitted as querystring values). When you use POST, the form data will be part of the request BODY.

In the ASP.NET MVC world, the attribute decorator [HttpPost] tells the framework that the action method (on which we used this decorator) is for handling a request coming from the client(browser) of type "POST" (the form method). This helps the framework to read the data from the request body and use that as needed (For Model binding etc)

In short, both are different things. One is one client side and other is on your server code. But you usually use it together to build a client side form and a server side method to handle the submitted form.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download