加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-core – 在ASP.NET MVC Core中仍然存在ViewModel概念?

发布时间:2020-12-16 04:01:57 所属栏目:asp.Net 来源:网络整理
导读:在以前的ASP.NET MVC版本中,您可以找到有关ViewModel的一些信息以及如何在此版本中使用它们. 我想知道为什么我在ASP.NET Core MVC中找不到关于这个主题的任何信息?这个概念是否仍然存在,如果需要,我需要把它们放在哪里? 问题出现了,因为我想为项目制作仪表
在以前的ASP.NET MVC版本中,您可以找到有关ViewModel的一些信息以及如何在此版本中使用它们.

我想知道为什么我在ASP.NET Core MVC中找不到关于这个主题的任何信息?这个概念是否仍然存在,如果需要,我需要把它们放在哪里?

问题出现了,因为我想为项目制作仪表板.项目是我的网络应用程序的主要入口点.他们有许多关系,例如里程碑.

楷模:

public class Project
{
    public int ProjectId { get; set; }
    public string Name { get; set; }

    public ICollection<Milestone> Milestones { get; set; }
    ...
}

public class Milestone
{
    public int MilestoneId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public DateTime Deadline { get; set; }
    public int? ParentId { get; set; }
    public Milestone Parent { get; set; }

    public ICollection<Milestone> Childrens { get; set; }
    ...
}

在ASP.NET Core之前,我创建了一个ProjectDashboardViewModel,用于获取视图的信息.我可以使用相同的方法吗?

解决方法

“这个概念是否仍然存在?” “我可以使用相同的方法吗?”

是的,ViewModel概念仍适用于.NET Core,您仍然可以像以前一样使用它们,即将选择的数据组合成符合特定视图需求的“形状”.

“我在ASP.NET Core MVC中找不到有关此主题的任何信息”

官方文档广泛讨论了视图模型. The Overview of ASP.NET Core MVC section有这个说:

Model Responsibilities

The Model in an MVC application represents the state of the
application and any business logic or operations that should be
performed by it. Business logic should be encapsulated in the model,
along with any implementation logic for persisting the state of the
application. Strongly-typed views will typically use ViewModel types
specifically designed to contain the data to display on that view; the
controller will create and populate these ViewModel instances from the
model.

In the Rendering HTML with views section:

You can pass data to views using several mechanisms. The most robust
approach is to specify a model type in the view (commonly referred to
as a viewmodel,to distinguish it from business domain model types),
and then pass an instance of this type to the view from the action. We
recommend you use a model or view model to pass data to a view.

The MVC/Advanced/Application Parts section还讨论了视图模型,the sample code there显示了如何将多个不同的对象组合在一起以供视图模型使用.

他们还提到了它们in the section on Partial Views.有一些示例代码与here一起使用,但这些示例实际上并没有真正突出模型和视图模型之间的区别.

通过以下文档搜索也更加突出了一些:https://docs.microsoft.com/en-us/search/index?search=viewmodel&scope=ASP.NET+Core

“..我想为项目制作一个仪表板”

在您的情况下,您提供的数据只显示一个具有一些子对象的域对象(“项目”).如果这是您要显示的所有数据,那么您可能不需要视图模型,因为它只是您的项目模型的镜像.

但是,如果要在项目仪表板上显示其他信息,例如一些数据汇总了有关正在进行的项目数量的数据,一个项目落后的项目列表等,然后您可以组装一个具有以下属性的视图模型:Project,NumberInProgressPrjects,OverdueProjectsList等.

public class ProjectDashboardViewModel
{
    public Project Project { get; set; }
    public int NumberInProgressProjects { get; set; }
    public ICollection<OverdueProjectInfo> OverdueProjectsList { get; set; }
}

这只是一个例子,重点是您可以使用视图模型来封装视图所需的所有数据,而不是您的控制器返回与单个域对象(通常是数据库中的表)匹配的模型对象,然后是批次在ViewData集合中使页面的其余部分功能所需的其他数据(例如填充下拉列表所需的数据).有关视图模型的很多优秀文章,例如this previous question covers them exhaustively,并且在.NET MVC Core中与其他版本的MVC一样重要.

“我需要把它们放在哪里?”

您可以将它们放在您选择的位置,只需确保在需要时使用using语句.较小项目中的典型约定是将它们放在名为“ViewModels”的文件夹中.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读