Asp.net mvc 2 自定义视图模型:验证属性会去哪里?

发布于 2024-09-13 06:31:13 字数 210 浏览 5 评论 0原文

到目前为止,我一直在顺利地使用在伙伴类上进行的数据注释。

当涉及到需要自定义视图模型的更复杂的视图时(例如,包含一些选择列表的视图模型)...我是否需要将验证属性传输到视图模型类?

我计划传递完整的自定义视图模型来填充“编辑”视图,但希望在“保存”操作中只接收一个简单的对象。

这个计划的缺陷在哪里,或者说整个事情本来就是一大堆失败?

谢谢。

I've been chugging along OK with the use of data annotations made on buddy classes so far.

When it comes to a more complex view that requires a custom view model--one that includes a few select lists, for example...would I need to then transfer my validation attributes to the view model class?

I was planning to pass the full custom view model to populate an "Edit" view, but was hoping to just receive a simple object in my "Save" action.

Where are the flaws in this plan, or is the whole thing just one big pile of fail in the first place?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浴红衣 2024-09-20 06:31:13

您仍在验证最终返回数据库的数据。因此,为了保持您的应用程序干燥,您最好使用 Buddy 类对于原始模型。

编辑
注意:这与您的问题没有任何关系

我个人更喜欢扩展原始模型以实现与“编辑”相关的任何内容,并且我更喜欢仅使用 ViewModel 进行“显示”(详细信息页面,列表页面等)。

示例:这是我的伙伴类,在其中我添加了一个在编辑页面显示中使用的“RegionName”属性,但它与数据库没有任何关系。您可以对要验证的自定义输入数据执行类似的操作,然后再将其操作为“数据库可用”数据。我在数据库中使用 RegionID,但我更喜欢使用访问者的友好名称,而不是 ID integer

<MetadataType(GetType(UserMetaData))> _ 
Partial Public Class User 
    Public RegionName As String 
End Class 

Public Class UserMetaData 

    <DisplayName("region name")> _ 
    <Required(ErrorMessage:="region name is required.")> _  
    Public Property RegionName As String 
End Class

You're still validating that data that is ultimately going back into the database. So in order to keep your application DRY, you are best off to use the Buddy Classes for the original Model.

Edit
note: this doesn't exactly have anything to do with your question

I personally prefer extend the original Model for anything "Edit" related and I prefer to use a ViewModel for "Display" only (Details pages, List pages, etc).

Example: here's my buddy class, and in it I've added a "RegionName" property that I use in the Edit Page display, but it doesn't have anything to do with the database. You can do a similar thing with custom input data that you want to validate before manipulating it into "database usable" data. I use the RegionID in the database, but I prefer to use the friendly name for the visitor instead of the ID integer

<MetadataType(GetType(UserMetaData))> _ 
Partial Public Class User 
    Public RegionName As String 
End Class 

Public Class UserMetaData 

    <DisplayName("region name")> _ 
    <Required(ErrorMessage:="region name is required.")> _  
    Public Property RegionName As String 
End Class
有深☉意 2024-09-20 06:31:13

您的视图模型仍将从基本模型继承验证。

Your view model will still inherit the validation from your base model.

人心善变 2024-09-20 06:31:13

不知道这是否有帮助,但我将验证属性放在我的模型中,这样无论我在哪里使用该模型,我都会得到相同的验证。我意识到对于某些项目来说并不理想。

实际上,我将属性放在分部类而不是模型中,因为 90% 的情况下,我的模型来自数据存储库中的 linq 2 sql 文件,

然后我的控制器只是检查模型是否有效,而视图不执行任何操作除了显示数据和错误之外。

不确定这是否是你要问的

Don't know if this helps but I put my validation attributes against my model so that wherever i use the model i get the same validation. not ideal for some projects i realise.

actually, i put the attributes agains a partial class rather than my model because 90% of the time my model comes from a linq 2 sql file in my data repository

my controller then simply checks if the model is valid or not and the view does nothing except display data and errors really.

unsure if this is what you're asking though

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文