6

I have created the view in MVC 4.0 and i have placed four text boxes and two submit buttons, one for create and one for cancel. Validation should be occur for the text boxes while i am clicking the create button. while clicking the cancel button, we need disable the validation. in ASP.NET, we can achieve this by setting the CausesValidation = false for the cancel button. in MVC 4.0, how we will achieve this? Could you please help me on this.

Thanks

3
  • make the class of that button as cancel Jul 2, 2014 at 10:21
  • You can simply use ActionLink to redirect user to different action. Jul 2, 2014 at 10:22
  • 1
    asp.net mvc validations only for work for input type submit not for input type button...so make cancel button input type button.
    – user2138919
    Jul 2, 2014 at 10:22

2 Answers 2

9

Just add class="cancel" to the button

For example,

<input type="submit" name="btnCancel" value="Cancel" class="cancel"/>

Updated

Use this link to identify which submit button has been pressed.

3
  • if i add "cancel" css class, it prevents the client side validation. in my case, i want to call the different action method in the same controller while clicking the cancel button. now it calls the same action method Jul 2, 2014 at 11:16
  • But, you specifically asked to avoid client side validation on cancel button. However, I have updated my answer and added link where you'll get idea how to do that. Jul 2, 2014 at 11:22
  • 1
    What does the class cancel do here? Is that a class jquery validation looks at before running client side validation?
    – Mustafa
    Oct 19, 2017 at 2:39
4

you don't have to make a cancel button submit instead you can make it link button

 @Html.ActionLink("Back","ActionName")

or you can make it <input type="button" value="Cancel" onclick="JsFunction()" /> the two cases they will not affect with validation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.