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; }

转载注明原文:c# – 如何在ASP.NET MVC中更改int模型验证的ErrorMessage? - 代码日志