【发布时间】:2016-12-13 15:15:09
【问题描述】:
我有一个模型,我在其上使用数据注释进行验证,如下所示:
public int Id { get; set; }
[Required(ErrorMessage = "Please enter the Sample Description.")]
public string Description { get; set; }
[Required(ErrorMessage = "Please enter the Start Date.")]
[DataType(DataType.Date)]
public DateTime StartDate { get; set; }
通过 API 插入数据,如果验证失败,我想将返回的任何错误消息直接显示到屏幕上。
这对于这样的事情很好:
[Required(ErrorMessage = "Please enter the Sample Description.")]
public string Description { get; set; }
但是在显示的整数属性“Id”和客户端发送的情况下,一个不能转换为整数的值,例如“x”,那么你会得到一个像这样的低级消息:
"Could not convert string to integer: x. Path 'Id', line 1, position 9.
我真正想要的是更像是一条消息
"Please provide an integer for the Id".
无论如何允许通过注释验证数据并且仍然提供比我得到的更有用的消息?
【问题讨论】:
标签: asp.net validation data-annotations