开发者

Problem with model binder property type int

开发者 https://www.devze.com 2023-04-07 17:44 出处:网络
In my View Model I have a property: [Required] [MaxLength(4)] [DisplayName(\"CVC\")] public int BillingCvc { get; set; }

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.


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)

0

精彩评论

暂无评论...
验证码 换一张
取 消