When I make an ajax call, it fails with 500 Internal server error
Value-typed properties marked as [Required] must also be marked with [DataMember(IsRequired=true)]
The problem is with CallerID property.
[Required]
public string AccountTypeID { get; set; }
[Required]
public int CallerID { get; set; }
If I mark CallerID as string, everything works well.
Any ideas why?
This is a known issue in Web API, you can see the whole history here:
http://aspnetwebstack.codeplex.com/workitem/270
Basically, if you apply [Required] to a value type (such as bool or int, string is not a value type) it will cause this error.
Also you need to think about it – you are making
inta required property – but as a value type it will always have value, value=0 even if it’s not passed by the user. Perhaps you were thinking ofint?instead?You can either remove the
InvalidModelValidatorProvideraltogether (which may not be acceptable though):Or just apply DataContract to your class:
One other workaround, mark the int as nullable: