2

in my MVC3 application, I've a form with 2 dropdownlists that aren't required. So, I haven't added any annotation to the related attributes on the partial class. Viewing the html code generated when I add a new object, no client validation code is added and all goes fine. The problem rises when I try to modify an object that has values on attributes related with the dropdownlists because automatically is added this code

data-val="true" data-val-number="The field IDCategory must be a number." data-val-required="The IDCategory field is required." id="ddlCategory" name="List.Category.IDCategory"

So, if on this object I try to set no value on the dropdownlists and after submit the form, it is fired the validation error. I think that the solution are 2:

  • Add some annotation on the related attribute to always avoid the generation of validation code
  • Add an attribute to the object in the view (like the cancel class on the buttons, I tried to add to them, but it doesn't works)

Thanks in advance!

stuzzo
  • 1,056
  • 1
  • 15
  • 36
  • How does your Edit model class look like? – WorldIsRound Mar 16 '11 at 22:07
  • Maybe this will help: http://stackoverflow.com/questions/4700172/unrequired-property-keeps-getting-data-val-required-attribute – frennky Mar 17 '11 at 09:07
  • @frennky: it seems what I'm looking for, but in my case it doesn't work :(, I don't why. I checked and the value is nullable and my db accept null values. Since my POCO classes are generated from the db, I don't why it adds the validation. Now I try to do some trick through jquery. – stuzzo Mar 18 '11 at 09:32
  • I noticed that the message is added to all the properties(five) in my form, but only the first two are edited with the Required attribute! I added this line DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false, but nothing! :/ – stuzzo Mar 18 '11 at 09:37

1 Answers1

1

You can always use JQuery to remove client-side validation class from a particular control.

$('#myControl').removeClass('myClass');
James Fleming
  • 2,589
  • 2
  • 25
  • 41
  • But if this is mvc added then won't there be server side validation too? – Brian White Oct 13 '12 at 00:54
  • Yes there will - or at least there should be! However you've got to decide how to best use it. You could decide that you want to post a single model then have custom logic to decide what is/isn't valid. Or you can post the form to two different methods, each which binds to a different version of the business object, each with it's own set of required fields. – James Fleming Oct 15 '12 at 12:58