6

I have an int? view model property that is validated at client-side as if it was required. That is, if I leave the field blank, it will not submit. The same does not happen for string properties.

The HTML rendered for my editor is:

<input type="text" value="" name="StatusIdSearch" id="StatusIdSearch" data-val-number="The field Status must be a number." data-val="true" class="text-box single-line">

I believe that data-val-number is causing an error because nothing is not a number, but I cannot determine why.

Any ideas?

Edit

The view-model:

public class CompromissoSearchModel
{
        // other properties removed for the sake of clarity

        [Display(Name = "Status")]
        [EnumDataType(typeof(StatusCompromisso))]
        public int? StatusIdSearch { get; set; }

       // other properties removed for the sake of clarity
}
Zach Green
  • 3,421
  • 4
  • 29
  • 32
Andre Pena
  • 56,650
  • 48
  • 196
  • 243
  • @SergioTapia: Thank you for taking a look at it. I updated the question – Andre Pena Jan 31 '12 at 18:20
  • 1
    Does the answer given here: http://stackoverflow.com/questions/4700172/unrequired-property-keeps-getting-data-val-required-attribute help? –  Jan 31 '12 at 18:26

3 Answers3

3

The message you are seeing it's not related to a required field validation. You're gettings this because ClientDataTypeModelValidatorProvider adds client numeric validation and it ignores if the type is nullable or nor not. You can check the code yourself:

private static IEnumerable<ModelValidator> GetValidatorsImpl(
    ModelMetadata metadata, 
    ControllerContext context) 
{
    Type type = metadata.RealModelType;
    if (IsNumericType(type)) {
        yield return new NumericModelValidator(metadata, context);
    }
}

And the IsNumericType implementation:

private static bool IsNumericType(Type type) 
{
    // strip off the Nullable<>
    Type underlyingType = Nullable.GetUnderlyingType(type); 
    return _numericTypes.Contains(underlyingType ?? type);
}

Since the nullable is not considered you always get that validation. In terms of solution, you need to remove ClientDataTypeModelValidatorProvider from the used providers or maybe replace it with a custom one that does not ignore nullable.

João Angelo
  • 56,552
  • 12
  • 145
  • 147
  • angelo: I'm still analyzing your answer. It's strange because I've just created a new MVC project and int? properties are not validate as required while [Required] properties are. There's something weird with this particular project as it seems it's not the expected behavior. – Andre Pena Jan 31 '12 at 19:33
1

You should be able to add the following code to your Application_Start method in Global.asax file to fix this issue:

DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;

Scott Smith
  • 1,823
  • 17
  • 15
0

I was having the exact same problem and managed to find a solution. None of these solutions worked for me so I thought I'd post my solution for anyone else having this problem.

The problem was not that the model binder was validating the field as invalid, but that when using TryUpdateModel the nullable property of the viewmodel wasn't nullable in the database entity.

Clearer explanation:

TryUpdateModel(dbUser, "", new[]{
    "DecimalProperty"
}));

"DecimalProperty" in the viewmodel was nullable, but it wasn't nullable in dbUser.

BlazeMan
  • 147
  • 3
  • 11