Master_T Master_T - 5 months ago 21
ASP.NET (C#) Question

ASP.net MVC form submission always calls GET action instead of POST

I have a form in ASP.net MVC that looks like this:

<div class="x_content">

<form id="elementEditForm" class="form-horizontal form-label-left">

<input type="hidden" name="returnUrl" value="@ViewBag.returnUrl" />


... my form fields....

<div class="form-group">
<div class="col-md-9 col-md-offset-3">
<button type="submit" form="elementEditForm" class="btn btn-success">Submit</button>
</div>
</div>
</form>
</div>


And in the controller I have:

[HttpGet]
public ActionResult Edit(string id, string returnUrl = null)
{
var e = getEditModel(id);
return View("GenericElementEdittView", e);
}

[HttpPost]
public ActionResult Edit(string id, object evm, string returnUrl = null)
{
log.Info(evm);

return View();
}


My problem is that the
HttpPost
method is not called when I click the button to submit the form. Instead, the
HttpGet
method gets called every time, resulting in the page just reloading instead of submitting.

What am I doing wrong?

Answer Source

GET is the default method when none is specified. Specify the POST method:

<form id="elementEditForm" class="form-horizontal form-label-left" method="post">