的ViewModels
[Display(Name = "Purchase limit")]
[Required]
public Dictionary<int, int> DownloadLimit { get; set; }
查看
@foreach (var downloadLimit in Model.DownloadLimit)
{
@Html.TextBoxFor(m => m.DownloadLimit[downloadLimit.Key], new { @class = "form-control mb10", @type = "number"})
}
我的字典有密钥,值都是整数。当我将字符串输入到文本框时,显示消息错误:“字段Int32必须是数字”。
RangeAttribute在Dictionary中不起作用。 我试过了:
[Range(0,99999999, ErrorMessage = "Download Limit must be a number in range from 0 to 99999999")]
public Dictionary<int, int> DownloadLimit { get; set; }
任何人都可以建议我这样做,我非常喜欢不使用自定义属性的解决方案。
谢谢!
答案 0 :(得分:4)
[Range(typeof(int), "0", "9999", ErrorMessage = "{0} must be a decimal/number between {1} and {2}.")]
或
[Range(typeof(decimal), "0", "9999", ErrorMessage = "{0} must be a decimal/number between {1} and {2}.")]