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 Commented Jul 2, 2014 at 10:21
  • You can simply use ActionLink to redirect user to different action. Commented 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. Commented 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.

Sign up to request clarification or add additional context in comments.

3 Comments

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
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.
What does the class cancel do here? Is that a class jquery validation looks at before running client side validation?
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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.