2

I have two submit buttons on my form, one to save, one to resolve:

            <input type="submit" name="submitButton" value="Save" class="btn btn-default" />
            <input type="submit" name="submitButton" value="Resolve" class="btn btn-default" />

I am dealing with these in the Controller like so:

  //POST: Exceptions
    [HttpPost]
    public ActionResult Edit(string submitButton, PersonViewModel personViewModel)
    {
        switch (submitButton)
        {
            case "Save":                    
                return (Save(person));
            case "Resolve":
                return (Resolve(person));
            default:                    
                return (View());
        }
    }

I know that I can make certain properties required by using data annotations on my view model, however I only want these particular properties to be required when the user attempts to Resolve, Save should have no such restrictions.

What is the best way of achieving this?

update

http://weblogs.asp.net/imranbaloch/overriding-unobtrusive-client-side-validation-settings-in-asp-net-mvc-3

The above blog seems to suggest that you can assign an input you wish to be excluded from validating prior to submit by giving it a class and adding some JS like so:

<input id="btnSubmitSave" type="submit" name="submitButton" value="Save" class="btn btn-default ignore" />
                    <input id="btnSubmitResolve" type="submit" name="submitButton" value="Resolve" class="btn btn-default" />

$(function () {
    var settngs = $.data($('form')[0], 'validator').settings;
    settngs.ignore = ".ignore";
});

Unfortunately this is still attempting to validate when I click the Save button.

JsonStatham
  • 9,770
  • 27
  • 100
  • 181
  • 1
    You must definitely try the answer in [this](http://stackoverflow.com/questions/5367287/disable-required-validation-attribute-under-certain-circumstances) question. – Farhad Jabiyev Mar 09 '15 at 17:27
  • I am already using a view model for this Edit page and I need to be able to Resolve and Save on this one page, so splitting the model up will not work for me here – JsonStatham Mar 09 '15 at 17:34
  • "save should have no such restrictions" - have you considered making your save button just a button rather than a submit button? – aw04 Mar 09 '15 at 18:22
  • Both of the submit buttons need to post the form data back to the server – JsonStatham Mar 09 '15 at 19:48
  • MVC uses jquery unobtrusive library for front end validation. So check out how to disable the validation on front end and use it for the case of resolve. while on server side you should only check for model state is valid if request is a save and ignore the state when it is resolve – A Khudairy Mar 10 '15 at 08:25
  • 1
    @SelectDistinct for future reference, you can still post to the server without a submit button. Just do a manual ajax post and serialize the form. – aw04 Mar 10 '15 at 12:35

2 Answers2

0

jQuery Validation plugin: disable validation for specified submit buttons

Sigh, it was as simple as giving my Save button the class of cancel wasted a few hours there.

Community
  • 1
  • 1
JsonStatham
  • 9,770
  • 27
  • 100
  • 181
0

Try using RequiredIf from:

https://foolproof.codeplex.com/

Exactly what you need

Lucas Roselli
  • 2,810
  • 1
  • 15
  • 18