Nate
Nate

Reputation: 2084

MVC3 and JQuery Ignore Validation

The following JQuery line lets me get passed validation but the serverside raises an error.

$(document).ready(function () {

    $("#save").click(function () {
        $("#WizForm").validate({
            onsubmit: false
        });
    });

How do I set the ModelState to be successful and allow me to continue to save my data as a draft entry? Right now I get the following error. Should I remove the rules? Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

Upvotes: 0

Views: 557

Answers (1)

counsellorben
counsellorben

Reputation: 10924

As Andrew Whitaker suggested, you should have two different controller actions, one for drafts (forms which fail validation), and one for final forms which pass validation. In addition, you should create an unvalidated model for your draft object.

Your client side script should be similar to the following:

$(document).ready(function() {
  $("#save").click(function () {
    if ($("wizForm").validate().form()) {
      // call controller action SaveFinal
    }
    else {
      // call controller action SaveDraft
    }
  }
}

Then, create a model without validation, and use this model in your SaveDraft controller action.

public ActionResult SaveDraft(UnvalidatedModel draft)
{
   // step through each field, and save only valid fields
}

counsellorben

Upvotes: 1

Related Questions