C# 在MVC中查看模型对象的域模型对象

C# 在MVC中查看模型对象的域模型对象,c#,asp.net-mvc,linq,entity-framework-5,automapper,C#,Asp.net Mvc,Linq,Entity Framework 5,Automapper,我想使用视图模型来显示,而不是域模型。我有以下视图模型类: public class ArticleDescriptionViewModel { public string Title { get; set; } public DateTime DateCreated { get; set; } } public class HomePage { public List<ArticleDescriptionViewModel> Articles { get;

我想使用视图模型来显示,而不是域模型。我有以下视图模型类:

public class ArticleDescriptionViewModel
{
    public string Title { get; set; }
    public DateTime DateCreated { get; set; }
}

public class HomePage
{
    public List<ArticleDescriptionViewModel> Articles { get; set; }
}
这种服务方式:

public List<ArticleDescription> GetArticlesDescription()
{
     var articleDescription= from a in _ctx.Articles
                             select new ArticleDescription 
                             { Title = a.Title, DateCreated = a.DateCreated };
     return articleDescription.ToList(); 
}
我有一个错误:

“无法将类型System.Collections.Generic.List(DBayonaCode.Domain.Services.Models.ArticleDescription)”隐式转换为“System.Collections.Generic.List(DBayonaCode.Models.ArticleDescriptionViewModel)”


但这两类是等价的吗?我做错了什么事。感谢您的帮助?

articlescription
articlescriptionviewmodel
是两种不同的类型,因此它们之间没有隐含的转换。您需要将域模型对象映射到视图模型对象,您可以手动或使用类似的工具进行映射

您可以编写如下扩展方法来进行映射:

public static class Mappings
{
    public static ArticleDescriptionViewModel ConvertToView(this ArticleDescription article)
    {
        // Mapping Code
        // return new ArticleDescriptionViewModel { ... }
    }

    public static List<ArticleDescriptionViewModel> ConvertToViews(this List<ArticleDescription> articles)
    {
        List<ArticleDescriptionViewModel> articleViews = new List<ArticleDescriptionViewModel>();

        foreach (ArticleDescription article in articles)
        {
            articleViews.Add(article.ConvertToView())
        }
        return articleViews;
    }
}
公共静态类映射
{
公共静态ArticleDescriptionViewModel ConvertToView(此ArticleDescription文章)
{
//映射代码
//返回新的ArticleDescriptionViewModel{…}
}
公共静态列表ConvertToViews(此列表文章)
{
List ArticleView=新建列表();
foreach(文章描述文章中的文章)
{
articleViews.Add(article.ConvertToView())
}
回访意见;
}
}

虽然MVC默认项目模板只提供一个模型文件夹,因此隐含了模型是一回事的想法,但实际上ASP.NET MVC应用程序中可能涉及三种类型的数据模型:

 - Domain model objects will be passed from and to a middle tier services interfacing with databases. 

- View Model objects are those that the Controller pass to the View.

- Input model objects are those that the default modelBinder or some  custom modelBinder  generates from the view, although in many cases the input models are the same view model objects.

希望有帮助。

这两个类不是等价的-它们可能具有相同的属性名,但它们不是相同的类。您需要将
articlescription
转换为
articlescriptionviewmodel
。使用linq
.Select(x=>newarticlescriptionviewmodel()){…
或者使用诸如感谢您的回答之类的工具。我将选择自动映射选项,但仍有一个问题:我在这里做的是好的方法吗,尝试实现关注点的分离?您知道,我可以在控制器中引用我的域类来创建一个类型列表以接收结果,这很简单我不需要映射这两个类。(域类-视图类)。?还有其他更好的方法吗?您的方法是正确的。您的控制器负责获取数据模型从服务并将其映射到视图模型。虽然您可以在视图中使用域模型,但最好使用视图模型仅显示/编辑您想要的内容,应用验证属性等。谢谢。这一点现在很清楚。我非常感谢您的帮助。谢谢您的回答。它工作得很好。但是仍然存在一个问题:它是g吗ood方法我在这里做什么,试图实现关注点的分离?你知道,我可以在我的控制器中引用我的域类来创建一个类型列表来接收结果,就是这样。我不需要映射这两个类。(域类-视图类).?还有其他更好的方法吗?使用视图模型是一种很好的做法,原因有很多,但主要是因为它通过保持视图模型中的验证、表示逻辑、数据格式等问题以及与域实体分离来保持域模型的清洁,此外,它们还可以防止出现问题。因此,我认为没有更好的方法来实现pass控制器和视图之间的数据。谢谢你的回答。还有一个问题:我在这里做的是好的方法吗,尝试实现关注点的分离?你知道,我可以在我的控制器中引用我的域类来创建类型列表以接收结果,就这样。我不需要映射这两个类ses.(域类-视图类)。?还有其他更好的方法吗?将视图模型与域模型区分开来对您有利。分离数据的方式是,您可以对与域模型无关的视图模型进行数据批注,从而利用[httpPost]上的ModelState.IsValid,并且您的域对象可能包含由应用程序的业务逻辑端生成的数据填充的属性,而不是由用户输入生成的属性,最后但并非最不重要的域模型可能包含不打算显示给用户的数据。谢谢。现在已经清楚了。我非常感谢您的帮助。
public static class Mappings
{
    public static ArticleDescriptionViewModel ConvertToView(this ArticleDescription article)
    {
        // Mapping Code
        // return new ArticleDescriptionViewModel { ... }
    }

    public static List<ArticleDescriptionViewModel> ConvertToViews(this List<ArticleDescription> articles)
    {
        List<ArticleDescriptionViewModel> articleViews = new List<ArticleDescriptionViewModel>();

        foreach (ArticleDescription article in articles)
        {
            articleViews.Add(article.ConvertToView())
        }
        return articleViews;
    }
}
 - Domain model objects will be passed from and to a middle tier services interfacing with databases. 

- View Model objects are those that the Controller pass to the View.

- Input model objects are those that the default modelBinder or some  custom modelBinder  generates from the view, although in many cases the input models are the same view model objects.