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

vellaichamy
  • 143
  • 1
  • 10

2 Answers2

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.

Community
  • 1
  • 1
Ashwini Verma
  • 7,477
  • 6
  • 36
  • 56
  • 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 – vellaichamy Jul 02 '14 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. – Ashwini Verma Jul 02 '14 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 '17 at 02: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

ASalameh
  • 783
  • 1
  • 8
  • 29