I have a simple form that is used to update an entity in my ASP.NET MVC app. I want the user to be able to submit the form, have the proper Action on my controller be called and then notify the user that the update was completed.
Because I wanted to display a JQuery dialog after the user clicks the update button, I wanted to use JQuery to submit the form. However, my problem is this:
While I can easily get JQuery to call an action on my controller, I can't get it to call an action that I've already setup in my route and that has been decorated with [Authorize]
and [AcceptVerbs(HttpVerbs.Post)]
For example, if the user is updating their entity at the following URL:
/SomeController/Edit/5
When JQuery submits the form, I want to submit it to my Edit Action that is already decorated and accepts the ID as a parameter.
The JQuery I am using to post the form is as follows:
$("#formFoo").submit(function() {
var frm = $("#formFoo");
var action = frm.attr("action");
var serializedForm = frm.serialize();
$.post(action, serializedForm, function() {
alert('Call completed');
});
return false;
});
If I define my form as follows:
<form id="formFoo" action="/SomeController/SomeOtherAction" method="post">
and my Action as follows:
public void SomeOtherAction()
{
var myEntity=new MyEntity();
UpdateModel(myEntity);
}
Then my action is called and my object is populated properly, but it's ID field isn't populated, so I don't know what entity I am updating.
If I change my form to:
<form id="formFoo" action="/SomeController/Edit" method="post">
and I have my Edit action defined as:
[Authorize,AcceptVerbs(HttpVerbs.Post)]
public void Edit(int id, FormCollection collection)
Then my action never seems to be called by JQuery.
I suppose I could somehow pull the ID parameter off of the querystring and stuff that into the data that I'm posting back to the form, but that seems very ugly. Or is there a way I can get the ID field while in in my SomeOtherAction action? Or am I just going about this all wrong?