asp.net-mvc-3 – 模型binder属性类型int的问题

在我的视图模型中,我有一个属性
[required]
    [MaxLength(4)]
    [displayName("CVC")]
    public int BillingCvc { get; set; }

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

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

当我发布表单我得到这个错误信息:

Unable to cast object of type 'system.int32' to type 'System.Array'.

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

解决方法

问题是您使用的类型为int的MaxLength。

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

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

相关文章

这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识...
Some samples are below for ASP.Net web form controls:(fr...
问题描述: 对于未定义为 System.String 的列,唯一有效的值...
最近用到了CalendarExtender,结果不知道为什么发生了错位,...
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence cha...