2

I've got a form done in Razor. The user can choose between two actions to take with the data they fill in to that form,

SaveCurrentProgress(User user)

...which allows the user to save the current state of the form and come back to it later. or:

SubmitFinalVersion(User user)

Which saves the form data and then renders it read-only.

I know how to use Html.BeginForm() to pass an object to a single action/controller, but how do I code it so the user can pass the object to one of the two actions?

Will
  • 103
  • 12
  • 2
    I think this question has the answer you are looking for: [How do you handle multiple submit buttons in ASP.NET MVC Framework?](http://stackoverflow.com/questions/442704/how-do-you-handle-multiple-submit-buttons-in-asp-net-mvc-framework) – Guy Kempsell Feb 24 '17 at 13:46
  • 1
    just add something in the data, which is passed to determine the process... so it will always hit action1 but it maybe passed to action2 in action1 depending on a value in the data. – Seabizkit Feb 24 '17 at 14:08

1 Answers1

1

You can't really. You could potentially manufacture some JS solution that would switch out the action based on what button is clicked, but as far as a basic form goes, there's no way to change the action conditionally.

However, you can name your submit buttons and then branch within a single action. For example:

<button type="submit" name="SaveProgress">Save Current Progress</button>
<button type="submit" name="SaveFinal">Save Final Version</button>

Then, in your one action:

if (Request["SaveProgress"] != null)
{
    // save progress
}
if (Request["SaveFinal"] != null)
{
    // save final
}

Since only the button that is clicked will make it into the post data, you can use the presence of the button's name to determine which the user clicked.

One final option is to simply not worry about saving the incomplete data server-side. You can utilize localStorage to save the entered data as the user adds it to the form. That means you wouldn't even need an explicit button for the purpose. However, the one downside to this approach is that it's necessarily bound to the browser and machine the user is currently editing on (since it's client-side). If they moved to a different computer, they would not be able to resume their edits.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444