0

I currently have a form using MVC 5 with two submit/post buttons--one to finalize the form (validate required fields among other validation), and one to just save the form (no real validation required). It works fine without including unobtrusive validation, but I'd like to use it...and when I do, the finalize button works okay, but the save button acts as though it's the finalize button--validating fields when it shouldn't be.

Is there a way to make this possible?

Code snippets are below.

View

{BeginForm code goes here}
@Html.LabelFor(model => model.TotalHours)
@Html.TextBoxFor(model => model.TotalHours)
@Html.ValidationMessageFor(model => model.TotalHours)
...
<input type="submit" value="Finalize" name="btnCreate" />
<input type="submit" value="Save and Quit" name="btnCreate" />

Model (using EF)

[Required(ErrorMessage = "Please enter the total number of hours.")]
[Display(Name = "Total Hours")]
public Nullable<decimal> TotalHours { get; set; }

Controller

[ValidateAntiForgeryToken]
[HttpPost]
[MultiBtn(Name = "btnCreate", Argument = "Finalize")]
public async Task<ActionResult> CreateFinalize(FormObj oForm)
{
    using (var dbContext = new DBContext())
    {
        if (ModelState.IsValid) { ... }
        else { return View(oForm); }
    }
}
[ValidateAntiForgeryToken]
[HttpPost]
[MultiBtn(Name = "btnCreate", Argument = "Save and Quit")]
public async Task<ActionResult> CreateFinalize(FormObj oForm)
{
    using (var dbContext = new DBContext())
    {
        // Remove model errors
        foreach (var modelStateVals in ModelState.Values)
            modelStateVals.Errors.Clear();
        // Process the form and save here
    }
}
Sparky
  • 98,165
  • 25
  • 199
  • 285
LOL. NO.
  • 577
  • 1
  • 6
  • 33

1 Answers1

0

I found that there's something in the html that you can add:

<input class="cancel" />

Unfortunately, the cancel class has been deprecated. But now it uses formnovalidate, which is what I needed.

LOL. NO.
  • 577
  • 1
  • 6
  • 33
  • **[Where do you see that the `cancel` class was deprecated?](https://github.com/jzaefferer/jquery-validation/releases)** `` will allow you to have a submit button that does not trigger validation. See: http://jsfiddle.net/267xptou/ – Sparky Jan 19 '16 at 19:52
  • I saw it in another StackOverflow answer (I don't have the link anymore unfortunately) from this site: http://jqueryvalidation.org/reference#link-skipping-validation-on-submit – LOL. NO. Jan 19 '16 at 19:56
  • Ok thanks. Clearly that's from the developer's documentation... strange that he forgets to mention it also within his [Release Notes](https://github.com/jzaefferer/jquery-validation/releases). – Sparky Jan 19 '16 at 21:47
  • More info: https://github.com/jzaefferer/jquery-validation/pull/623 – Sparky Jan 19 '16 at 21:52