So this is my controller:
public class MyAccountController : Controller
{
public ActionResult MyAction(int id = 1)
{
return View();
}
[HttpPost]
public ActionResult MyAction(FormCollection values)
{
return Content(values["something"]);
}
}
and this is my view:
@using (Html.BeginForm())
{
<input type="hidden" name="something" value="something" />
<input type="submit" />
}
It’ll work as expected, if you click the submit button it’ll redirect the page and will show “something”
But when I change my View to this:
<button id="button">submit</button>
<script type="text/javascript">
$("#button").on("click", function (e) {
$.post("/MyAccount/MyAction", {something:"something"})
});
</script>
It won’t return the “Content” to the browser, I’ve debugged the application, and it gets into my post action, but when it arrives to “return Content(values[“something”]);” it doesn’t do anything.
Is it jQuery somehow preventing my app to redirect the page?
So as @SLaks and @bhamlin said $.post is an ajax call. And you have to handle what it gets back from the server. so this is what I did: