Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>My solution was to do two things. Say we have a Save button and another Add Something button. When user clicks on Save we want client validation and server validation to be performed. For later button we don't want any validation to take place.</p> <ol> <li>Temporarily disable client validation for second button (on click):</li> </ol> <blockquote> <p>&lt;input type="submit" name="submit-button" value="Save" /&gt;</p> <p>&lt;input type="submit" name="submit-button" value="Add Something" onclick="document.forms[0].noValidate = true; document.forms[0].submit();" /&gt;</p> </blockquote> <p>Good thing about it is when JavaScript is disabled the client validation would never have taken place anyway.</p> <ol> <li>Take care of server side</li> </ol> <p>Similar to what Bryan is saying when you click any submit button within a form, the entire form and the clicked submit button value is posted. You can differentiate which button was clicked by the name posted. In example above when user clicks on Save button and we read Request.Form["submit-button"] in controller post action we get "Save". If user clicked on Add Something we would get "Add Something". This is the way HTML is supposed to work.</p> <p>Now to get around having magic strings all over the place I usually have a public static class within the controller, like so:</p> <pre><code>public class HomeController { public static class Buttons { public const string Save = "Save"; public const string AddSomething = "Add something"; } // Action methods } </code></pre> <p>So you can use these for rendering form:</p> <pre><code>&amp;lt;input type="submit" name="submit-button" value="@HomeController.Buttons.Save" /&amp;gt; </code></pre> <p>And you can easily read the button clicked in controller:</p> <pre><code>[HttpPost] public ActionResult Index(Model viewModel) { var buttonClicked = Request.Form["submit-button"]; switch (buttonClicked) { case HomeController.Buttons.Save: return Save(viewModel); case HomeController.Buttons.AddSomething: return AddSOmething(viewModel); } return View(); } </code></pre> <p>In Save method you first ask if ModelState.IsValid and return view model if not but in AddSomething method we will clear any errors:</p> <pre><code>public ActionResult AddSomething(Model viewModel) { ModelState.Clear(); // your code to add something to model return View(viewModel); } </code></pre> <p>This was you keep everything clean, tidy and testable. And you can introduce a constant for submit-button html name attribute. It might be possible to do all the constants with T4MVC too. A similar solution applies to when you need a "auto postback" combo box, except you need a hidden field that is set via onchange event of the select element.</p> <p>Hope this helps.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload