Problem with model binder property type int

asp.net-mvc-3

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.

Best Answer

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)

Related Topic