如何为自定义视图模型重用模型元数据?
我正在开发一个 ASP.NET MVC 2 项目,其中包含一些应用了元数据数据注释属性的业务实体(验证属性、显示属性等)。
像这样的事情:
//User entity
public class User
{
[DisplayName("Vorname")]
[Required(ErrorMessage = "Vorname fehlt")]
[StringLength(MaxNameLength, ErrorMessage = "Vorname ist zu lang")]
public string FirstName { get; set; }
[DisplayName("Nachname")]
[Required(ErrorMessage = "Nachnamefehlt")]
[StringLength(MaxNameLength, ErrorMessage = "Nachname ist zu lang")]
public string LastName { get; set; }
[Required]
public string Password{ get; set; }
}
使用来自不同视图的元数据是没有问题的,只要我使用我的业务实体作为视图模型或作为视图模型的一部分,如下所示:
//custom viewmodel with a user entity
public class CustomViewModel
{
public User{get;set;}
//some more properties...
}
但是,有时我需要编写一个视图来编辑一些字段,但不是所有字段一个实体的。对于这些字段,我想重用用户实体中已指定的元数据。其他字段应被忽略。我正在谈论这样的自定义视图模型:
[MetadataType(typeof(User))]
public class UserNameViewModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
//no password on purpose, the user should only
//edit his first and last name in this view
}
这就是我遇到问题的地方。上面的自定义视图模型在生成视图时会导致异常,因为它没有密码属性。
类型的关联元数据类型 'Zeiterfassung.Models.ViewModels.Users.UserNameViewModel+UserModel' 包含以下未知内容 属性或字段:密码。请确保 这些成员的名字匹配 上的属性名称 主要类型。
另外,即使没有发生此异常,我预计在表单提交时的模型验证方面会遇到更多麻烦,因为密码在我的业务实体中被标记为必需。
我可以想到几种解决方法,但似乎没有一个是真正理想的。无论如何,我无法更改数据库布局,以便密码字段位于上面示例中的单独实体中。
你会如何处理这种情况?
I'm working on an ASP.NET MVC 2 project with some business entities that have metadata dataannotations attributes applied to them (Validation attributes, Display attributes, etc.).
Something like:
//User entity
public class User
{
[DisplayName("Vorname")]
[Required(ErrorMessage = "Vorname fehlt")]
[StringLength(MaxNameLength, ErrorMessage = "Vorname ist zu lang")]
public string FirstName { get; set; }
[DisplayName("Nachname")]
[Required(ErrorMessage = "Nachnamefehlt")]
[StringLength(MaxNameLength, ErrorMessage = "Nachname ist zu lang")]
public string LastName { get; set; }
[Required]
public string Password{ get; set; }
}
Using the metadata from different views is no problem, as long as I am using my business entities as viewmodels or as part of a viewmodel like this:
//custom viewmodel with a user entity
public class CustomViewModel
{
public User{get;set;}
//some more properties...
}
However, sometimes I need to code a view for editing some, but not all fields of an entity. For those fields I want to reuse the metadata already specified in my user entity. The other fields should be ignored. I'm talking about custom view models like this:
[MetadataType(typeof(User))]
public class UserNameViewModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
//no password on purpose, the user should only
//edit his first and last name in this view
}
That's where I am running into problems. The custom view model above leads to an exception when the view is generated, because it has no password property.
The associated metadata type for type
'Zeiterfassung.Models.ViewModels.Users.UserNameViewModel+UserModel'
contains the following unknown
properties or fields: Password. Please make sure
that the names of these members match
the names of the properties on the
main type.
Also, even if this exception did not occur, I expect to get into even more trouble with model validation on form submit because Password is marked as required in my business entity.
I can think of several workarounds, but none seem really ideal. In any case I can't change the database layout so that the password field would be in a separate entity in my example above.
How would you handle this scenario?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可以给您的唯一建议是让视图模型特定于每个视图,并且在这些视图模型上仅具有必要的属性和验证属性。如果您在视图模型上重复某些验证属性和属性,请不要担心。这就是它们的目的:反映给定观点的逻辑。
The only recommendation I could give you is to have view models specific to each view and have only the necessary properties and validation attributes on those view models. Don't worry if you repeat some validation attributes and properties on your view models. That's what they are meant for: reflect the logic of a given view.