What should the action return when submitting multiple forms in an MVC app?
I have a form with multiple tabs. Each t开发者_运维百科ab has its own <form>
, defined like this:
<% Using Html.BeginForm("Save", "FirstSection")%>
I've got a master Save button that submits all the forms on the page like this:
$("form").each(function() { $(this).submit(); });
The problem is that I don't know what each Save action should return:
<AcceptVerbs(HttpVerbs.Post)> _
Function Save(ByVal data As MyTable) As ActionResult
SaveTheDataHere()
Return View()
End Function
If I use Return View()
it's going to fail because I don't have a "get" equivalent for the Save action. What I really want the post to do is nothing - just save the data and be done.
Is there a better return value to use?
This:
$("form").each(function() { $(this).submit(); });
Is not a good idea. First submision will reload the page and the other forms might not be submitted. The better solutions:
- Place all form controls in one form and use javascript to show/hide active tab. Only one submit button will submit all the tabs data.
- Use different view for each tab.
You could return a simple JsonResult with a status message. This would be good practice anyway, since then you would know if something failed with your form posts.
public JsonResult Save()
{
SaveTheDataHere()
return Json("Success");
}
Sorry for the C#. My VB is rusty.
PanJanek is right
also
You could use ajax and json with jQuery it's very easy
but it seems like you like your current process, you could
return View("namedView") or redirectToAction("actionName")
精彩评论