I have a simple form that is used to update an object in my ASP.NET MVC application. I want the user to be able to submit the form, invoke the appropriate action on my controller and then notify the user that the update is complete.
Since I wanted to display the jQuery dialog after the user clicked the refresh button, I wanted to use jQuery to submit the form. However, my problem is this:
Although I can easily get JQuery to trigger an action on my controller, I can't get it to trigger an action that I already set on my route and that was decorated [Authorize]and[AcceptVerbs(HttpVerbs.Post)]
For example, if a user updates their entity at the following URL:
/SomeController/Edit/5
When jQuery submits a form, I want to submit it to my editing action, which is already decorated, and takes the identifier as a parameter.
The jQuery that I use to submit 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 is as follows:
public void SomeOtherAction()
{
var myEntity=new MyEntity();
UpdateModel(myEntity);
}
Then my action is called and my object is populated correctly, but its identifier field is not filled, so I do not know which object I am updating.
If I change my form to:
<form id="formFoo" action="/SomeController/Edit" method="post">
and I have an Edit action defined as:
[Authorize,AcceptVerbs(HttpVerbs.Post)]
public void Edit(int id, FormCollection collection)
Then my action never calls jQuery.
I suppose I could somehow pull the ID parameter out of the query string and pass in the data that I send back to the form, but that seems very ugly. Or is there a way I can get the ID field during SomeOtherAction action? Or am I just about this wrong?