在我的应用程序中,我的模型结构用 DataAnnotations 装饰。这对我的验证非常有帮助,但是我不确定如何让这些 DataAnnotations 在不重复输入的情况下持久保存到我的 ViewModels。
基本上我很懒,我尽量让它保持干爽。
class User
{
[Required]
public string FirstName {get; set; }
[Required]
public string LastName {get; set; }
public datetime RegistrationDate {get; }
}
class CreateUserViewModel
{
public string FirstName {get; set; }
public string LastName {get; set; }
}
第一个类永远不会被 View 使用,但它包含应用程序所需的所有 DataAnnotations。第二类始终由 CreateUser View 使用,但我不想重新应用 DataAnnotations。这可能吗?如果是,怎么办?
最佳答案
参见 MetadataType允许您将 DataAnnotations 移动到单独的类的属性。
关于asp.net-mvc - 如何让 DataAnnotations 持久化到 ViewModels,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5902828/