我有一个模型,其属性定义如下:

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

09-25 18:08