【发布时间】:2016-06-23 17:13:03
【问题描述】:
我的 web api 中有以下模型
public class MyModel
{
[Required(ErrorMessage = "Date is required")]
[ValidDate(ErrorMessage = "Not a valid date format")]
[ValidStartDate(ErrorMessage = "Not a valid start date")]
public string startDate { get; set; }
[Required(ErrorMessage = "Date is required")]
[ValidDate(ErrorMessage = "Not a valid date format")]
[IsDateAfterOrOnStartDate("startDate", ErrorMessage = "end date must be greater than or equal to start date")]
public string endDate { get; set; }
}
我在自定义属性 ValidDateAttribute、ValidStartDateAttribute 和 IsDateAfterOrOnStartdateAttribute 中实现了一些自定义逻辑。但我想要的是,如果 ValidDateAttribute(在两个属性上)给出错误或模型验证失败。我不希望框架在 startDate 属性上执行 ValidStartDateAttribute 并在 endDate 属性上执行 IsDateAfterOrOnStartDateAttribute,换句话说,我希望模型验证在不进一步传播和执行这些属性上的其他属性的情况下自行停止它。
有没有我可以达到预期的结果?
【问题讨论】:
标签: c# asp.net-web-api data-annotations