1

I have a Razor view with multiple buttons. I only want validation to occur when the user clicks one of these buttons.

How do I stop or clear validation of fields in the controller?

Michael Edwards
  • 6,308
  • 6
  • 44
  • 75
  • possible duplicate of [Disable client-side validation in MVC 3 "cancel" submit button](http://stackoverflow.com/questions/5363624/disable-client-side-validation-in-mvc-3-cancel-submit-button) – jgauffin Feb 23 '12 at 10:52
  • that post talks about stopping client side validation. I need to clear/stop server side validation – Michael Edwards Feb 23 '12 at 11:00
  • did you try to add the attribute? – jgauffin Feb 23 '12 at 11:01
  • I haven't, what is the name of the attribute? – Michael Edwards Feb 23 '12 at 11:12
  • As described in the linked answer: `class="cancel"` – jgauffin Feb 23 '12 at 11:14
  • I thought you meant an attribute on the method in the controller, not a class on the button. Adding a class to the button will only disable client side validation, not server side validation. – Michael Edwards Feb 23 '12 at 11:19
  • Ah. in the controller. I missed that part. Why would you want to POST if validation fails? Anyone could disable the validation then. Use another controller action instead and authorize the users that use it, and simply don't validate in it. – jgauffin Feb 23 '12 at 11:20
  • The form has to work without JS enabled, so client side validation one happen. The problem I have is that the data is posted to the action but I am using Data Annotations to mark fields for validation. This validation occurs before it gets to the controller method that is being called. I was wondering if there is a setting that will allow me to disable the validation for that method, or clear validation that has happened – Michael Edwards Feb 23 '12 at 11:30
  • http://stackoverflow.com/questions/1775170/asp-net-mvc-modelstate-clear – jgauffin Feb 23 '12 at 11:34
  • Thanks, a combination of ModelState.Clear and a custom attribute resolved my problem. Do you want to put this as an answer? – Michael Edwards Feb 23 '12 at 11:53

1 Answers1

1
  1. To disable client side validation:

    Disable client-side validation in MVC 3 "cancel" submit button

  2. To disable server side validation:

    Asp.net MVC ModelState.Clear

Community
  • 1
  • 1
jgauffin
  • 99,844
  • 45
  • 235
  • 372