加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 00:39:48 所属栏目:asp.Net 来源:网络整理
导读:在我的视图模型中,我有一个属性: [Required] [MaxLength(4)] [DisplayName("CVC")] public int BillingCvc { get; set; } 在我看来,我使用它像这样: @Html.TextBoxFor(x = x.BillingCvc,new { size = "4",maxlength = "4" }) 当我发布表单我得到这个错误
在我的视图模型中,我有一个属性:
[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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读