简体   繁体   English

元数据类与asp.net MVC中的ViewModel是否相同?

[英]Are Metadata classes same as ViewModel in asp.net MVC?

I'm a little confused about this one. 我对此有些困惑。 I read about metadata classes in this article on MSDN. 我在MSDN上的这篇文章中阅读了有关元数据类的信息。

It says that the reason for creating metadata is not to mess with the auto generated models by EF. 它说创建元数据的原因不是要弄乱EF自动生成的模型。

So this is a model generated by EF: 因此,这是由EF生成的模型:

namespace Blog.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Article
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Article()
        {
            this.ArticleTags = new HashSet<ArticleTag>();
            this.Comments = new HashSet<Comment>();
        }

        public int ArticleID { get; set; }
        public string PostTitle { get; set; }
        public string PostContent { get; set; }
        public string PostLinkText { get; set; }
        public Nullable<System.DateTime> PostDateTime { get; set; }
        public Nullable<int> PostAuthorID { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<ArticleTag> ArticleTags { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Comment> Comments { get; set; }
        public virtual Admin Admin { get; set; }
    }
}

and this is metadata class for Article model: 这是Article模型的元数据类:

public class ArticleMetadata
    {
        [Display(Name = "Post Title")]
        public string PostTitle { get; set; }

        [Display(Name = "Content")]
        public string PostContent { get; set; }

        [Display(Name = "Link Text")]
        public string PostLinkText { get; set; }

        [Display(Name = "Post Date and Time")]
        public DateTime? PostDateTime { get; set; }

        [Display(Name = "Post Author")]
        public int? PostAuthorID { get; set; }
    }

linked to model class using PartialClasses.cs: 使用PartialClasses.cs链接到模型类:

[MetadataType(typeof(ArticleMetadata))]
public partial class Article
{
}

Is the metadata class the same as a ViewModel?? 元数据类是否与ViewModel相同?

If so, how are these different and which one should be used in my case? 如果是这样,这些有什么不同?在我的情况下应该使用哪一个?

No, they aren't the same. 不,他们不一样。

Metadata classes allow you to define/add restraints to your class members. 元数据类允许您为类成员定义/添加约束。

View models are usually used to make it easier to use your model in a view. 视图模型通常用于简化在视图中使用模型的过程。 Such as having lists of SelectListItems for dropdowns, having properties to accept form post values, etc. 例如具有下拉列表的SelectListItems列表,具有接受表单发布值的属性等。

The two are usually used in conjunction, you add validation in your metadata class, and enforce it through the view model. 两者通常结合使用,您可以在元数据类中添加验证,然后通过视图模型强制执行验证。

In your specific case, it seems all you need is a metadata class. 在您的特定情况下,您似乎只需要一个元数据类。

class ArticleMetadata is a helper class for class Article , needed because you can't otherwise add those Annotation attributes to specific properties. class ArticleMetadata类是class Article类的帮助类,是必需的,因为您不能以其他方式将这些Annotation属性添加到特定属性。
Together they form the Model part of MVC. 它们共同构成了MVC的模型部分。

Note that the partial class can't help with the properties because it can't redefine them. 请注意,分部类无法帮助使用属性,因为它无法重新定义属性。 It is used here only to link up the MetaData class. 在此仅用于链接MetaData类。

For very simple operations (CRUD pages) you can use the Model directly. 对于非常简单的操作(CRUD页面),您可以直接使用模型。 In all other case, create a ViewModel for each View. 在所有其他情况下,为每个视图创建一个ViewModel。 The Article Model will probably be used by an EditArticleViewModel. ArticleModel可能由EditArticleViewModel使用。

In general you should create ViewModels to support Views, not one for each Model. 通常,您应该创建ViewModels来支持View,而不是每个Model都支持一个ViewModel。 One ViewModel could be composed of data from several Model classes. 一个ViewModel可由几个Model类的数据组成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM