Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting MaxLength gives you error

Following is one of the property in my MVC model.

[Display(Name = "Event ID")]
[MaxLength(8, ErrorMessage = "Event ID can be of maximum 8 characters long")]
[Required(ErrorMessage="Event ID must be entered")]
public Nullable<int> ID_EVENTO { get; set; }

I have bound the model with a View, and when I try to click "Submit" button, it gives following runtime error -

Unable to cast object of type 'System.Int32' to type 'System.Array'

While, if I remove the "MaxLength" attribute, it starts working.

What could be the issue here?

like image 231
Nirman Avatar asked Jun 21 '13 10:06

Nirman


1 Answers

MaxLength is used to specify the maximum length of array or string data allowed in a property.

Your ID_EVENTO is a nullable int (rather than array or string), that's why the attribute doesn't work. Sounds like you either want to remove the attribute or use a different one - Range or something?

like image 177
andreister Avatar answered Oct 20 '22 16:10

andreister