views:

65

answers:

3

I have a form with multiple tabs. Each tab 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?

A: 

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.

womp
+3  A: 

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:

  1. 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.
  2. Use different view for each tab.
PanJanek
The problem is that I'm only loading tabs when they are viewed, because loading all the tabs all the time slows everything down. If I have one big form, not all the fields will be loaded, so my Save will think the unloaded fields should be blanked out.
gfrizzle
In that case your Save action has to be smart enough to see the difference between blank field and the field that doesn't exist in the form. Default model binder in MVC does that - when you call in your controller UpdateModel<MyModel>(model) only the fields existing in the request are updated. You can load only first tab by default, then extend the form with ajax each time the user will click some new tab, and post entire form with one submit button. Model binder will update only the existing fields.
PanJanek
A: 

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")

freddoo