模型绑定器属性类型int的问题

The*_*Man 18 asp.net-mvc-3

在我的View Model中,我有一个属性:

    [Required]
    [MaxLength(4)]
    [DisplayName("CVC")]
    public int BillingCvc { get; set; }
Run Code Online (Sandbox Code Playgroud)

在我看来,我这样使用它:

@Html.TextBoxFor(x => x.BillingCvc, new { size = "4", maxlength = "4" })
Run Code Online (Sandbox Code Playgroud)

当我发布表单时,我收到此错误消息:

Unable to cast object of type 'System.Int32' to type 'System.Array'.
Run Code Online (Sandbox Code Playgroud)

但是,如果我将属性更改为字符串而不是int,我不会得到错误.将其声明为int允许客户端验证程序检查该字段是否包含非数字.

Dav*_*ick 36

问题是你使用的MaxLength是一种类型的int.

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

编辑:你;可能正在寻找 Range(int,int)