title: How to best implement Save | Save and Close | Cancel form actions in ASP.NET MVC 3 RC
body: <p>I'm wondering how you might go about implementing multiple form actions when submitting a form in asp.net mvc 3 RC.</p>
  
  <p>If I'm editing a user, for example I would like to have an action bar with the following buttons;</p>
  
  <p><strong>"Save" | "Save and Close" | "Cancel"</strong></p>
  
  <p>Save - Submits the form and saves, returning you to the edit screen. Could be easily implemented as a standard input/submit button. Nothing special here.</p>
  
  <p>Controller code for this might look like</p>
  
  <pre><code>public ActionResult Edit(UserViewModel model)
  {
    ...
    return RedirectToAction("Edit", model.Id");
  }
  </code></pre>
  
  <p>Cancel - Just returns you to previous screen. I was thinking about using an anchor tag for this.</p>
  
  <pre><code>&lt;a href="@Request.UrlReferrer" class="button"&gt;Cancel&lt;/a&gt;
  </code></pre>
  
  <p>But I'm stumped on how to implement <strong>"Save and Close"</strong> when you need to submit the same form data. I was wondering about having a nullable close param maybe?</p>
  
  <pre><code>public ActionResult Edit(UserViewModel model, bool? close)
  {
    ...
    return  close.GetValueOrDefault(false) ? RedirectToAction("Index", model.Id" : RedirectToAction("Edit", model.Id");
  }
  </code></pre>
  
  <p>But how do I submit this extra param along with the form in this case?</p>
  
  <p>If possible, I'd like to have a single form action for handling the submit as in the above mockup.</p>
  
  <p>I'm also interested if anyone else has come up with a nice user interaction model around this idea.</p>
  
  <p><strong>Solution</strong></p>
  
  <p>I ended up using Omar's suggestion below but instead of passing in a string I took in an enum so I don't have to do string checks in all my controllers.</p>
  
  <pre><code>public ActionResult Edit(UserViewModel model, FormAction actionType)
  {
    // pre-check
    if (actionType == FormAction.Cancel)
       // just return user to previous view and don't save.
  
    // Save code
  
    if (actionType == FormAction.Save)
       return ...
    else if (actionType == FormAction.SaveAndClose)
       ....
  }
  </code></pre>
  
  <p>Because I wanted a friendlier "Save and Close" text on the <code>&lt;input&gt;</code> button but wanted to use an enum I implemented a custom ModelBinder for FormAction that did the parsing.</p>
  
  <p>I didn't use a <code>&lt;button&gt;</code> tag because the theming was already in place for <code>&lt;input&gt;</code> tags.</p>