【发布时间】:2010-05-11 19:52:14
【问题描述】:
给定以下类:
using System.ComponentModel.DataAnnotations;
public class Book{
public Contact PrimaryContact{get; set;}
public Contact SecondaryContact{get; set;}
[Required(ErrorMessage="Book name is required")]
public string Name{get; set;}
}
public class Contact{
[Required(ErrorMessage="Name is required")]
public string Name{get; set;}
}
有没有一种干净的方法可以使用DataAnnotations 为Book 中的Contact 的每个实例提供不同的错误消息?例如,如果PrimaryContact 实例中缺少姓名,则错误将显示为“需要主要联系人姓名”。
我当前的解决方案是创建一个验证服务来检查模型状态中的字段错误,然后删除所述错误并使用我想要的特定语言将它们添加回来。
【问题讨论】:
标签: c# asp.net-mvc validation annotations data-annotations