c# – 如何在ASP.NET MVC中更改int模型验证的ErrorMessage?

我有一个具有这样定义的属性的模型:

    [Required(ErrorMessage="Please enter how many Stream Entries are displayed per page.")]
    [Range(0,250, ErrorMessage="Please enter a number between 0 and 250.")]
    [Column]
    public int StreamEntriesPerPage { get; set; }

除非用户输入“100q”这样的东西,否则这是可行的。然后显示一个相当难看的错误,表示“值”100q“对StreamEntriesPerPage无效。”

有没有一个属性我可以用来覆盖默认错误消息,当输入不是一个int?

是的,您可以使用Data annotations extensions,标记您的财产如下:

[Required(ErrorMessage = "Please enter how many Stream Entries are displayed per page.")]
[Range(0, 250, ErrorMessage = "Please enter a number between 0 and 250.")]
[Column]
[DataAnnotationsExtensions.Integer(ErrorMessage = "Please enter a valid number.")]
public int StreamEntriesPerPage { get; set; }
http://stackoverflow.com/questions/6587816/how-to-change-the-errormessage-for-int-model-validation-in-asp-net-mvc

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:c# – 如何在ASP.NET MVC中更改int模型验证的ErrorMessage?