RSS

Validation Was Seen More Than Once: Implicit Required Attribute

20 Feb

I recently encountered a duplicate validation error in ASP.Net MVC. At first, this error is mystery to me because it still gave me exception error after I disabled ALL my validations.

validation-was-seen-more-than-once-implicit-required-attribute

Turns out, this is an easy fix. Root cause is by default MVC set AddImplicitRequiredAttributeForValueTypes property to true. This basically means validation is automatically performed on value type. In my case, I have CountryId as follow:

public class Country {
    public int CountryId { get; set; }
}

Since CountryId is not nullable, MVC add require validation for the property.

To fix this, you can either set your value type to nullable or set AddImplicitRequiredAttributeForValueTypes to false.

Nullable Value Type

Notice the int is now int?

public class Country {
    public int? CountryId { get; set; }
}

DataAnnotationsModelValidatorProvider

Set AddImplicitRequiredAttributeForValueTypes to false in your Global.asax.cs.

DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;

Resource: MSDN

Advertisements
 
1 Comment

Posted by on February 20, 2013 in General

 

Tags: ,

One response to “Validation Was Seen More Than Once: Implicit Required Attribute

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

 
%d bloggers like this: