Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with model binder property type int

In my View Model I have a property:

    [Required]
    [MaxLength(4)]
    [DisplayName("CVC")]
    public int BillingCvc { get; set; }

In my view I use it like so:

@Html.TextBoxFor(x => x.BillingCvc, new { size = "4", maxlength = "4" })

When I post the form I get this error message:

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

However, if I change the property to string instead of int, I don't get the error. Declaring it as int allows the client validator to check if the field contains non numbers.

like image 296
The Muffin Man Avatar asked Sep 25 '11 22:09

The Muffin Man


1 Answers

The issue is your use of MaxLength with a type of int.

see: http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.maxlengthattribute(v=vs.103).aspx

edit: you;re probably looking for Range(int,int)

like image 197
David Wick Avatar answered Sep 28 '22 05:09

David Wick