0

I've a ViewModel with a property whose type is Double. The property doesn't have the [Required] attribute though in the front-end the validation wants it to have a value. To be clear, this captures a Vehicle's condition after set days and sometimes it's a new car and doesn't have a service record.

C#:

[Display( Name = "Last Service was on KILOs" )]
public Double LastServiceMileage { get; set; }

HTML:

<input class="input-validation-error" data-val="true" data-val-number="The field Last Service was on KILOs must be a number." data-val-required="The Last Service was on KILOs field is required." id="LastServiceMileage" name="LastServiceMileage" placeholder="On Kilo's" value="0" type="text">
<span class="field-validation-error" data-valmsg-for="LastServiceMileage" data-valmsg-replace="true"><span class="" generated="true" for="LastServiceMileage">The Last Service was on KILOs field is required.</span></span>

As you can see, it has generated an input field that has [data-val-number] and [data-val-required]. I guess it's only supposed to have [data-val-number] which should only validate for numbers if someone enters a value in the field. Anyone know how I can make it not validate this NOT required field?

Morgs
  • 1,558
  • 3
  • 19
  • 34

1 Answers1

1

Try adding this to your Application_Start

DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
Grizzly
  • 5,873
  • 8
  • 56
  • 109