gpt4 book ai didi

c# - ViewModel概念仍然存在于ASP.NET MVC Core中吗?

转载 作者:行者123 更新时间:2023-12-03 14:41:14 26 4
gpt4 key购买 nike

在ASP.NET MVC的早期版本中,您可以找到有关ViewModel以及在此版本中如何使用它们的一些信息。

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

因为我想为项目创建仪表板,所以出现了问题。项目是我的Web应用程序的主要入口点。他们有很多关系,例如与里程碑。

楷模:

    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来获取 View 信息。我可以使用相同的方法吗?

最佳答案

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

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

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

官方文档广泛讨论了 View 模型。 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还讨论了 View 模型,the sample code there显示了如何将许多不同的对象组装在一起,以供具有 View 模型的 View 使用。

他们还提到了in the section on Partial Viewshere附带了一些示例代码,但是这些示例实际上并没有真正突出显示模型和 View 模型之间的区别。

如下搜索文档也会突出显示更多内容:https://docs.microsoft.com/en-us/search/index?search=viewmodel&scope=ASP.NET+Core

“..我要为项目创建仪表板”

在您的情况下,您提供的数据仅显示单个域对象(“项目”),其中包含一些子对象。如果这就是您要显示的所有数据,那么您可能不需要 View 模型,因为它只是Project模型的镜像。

但是,如果您想在“项目”仪表板上显示其他信息,例如一些数据汇总了有关正在进行中的项目数,哪些项目在后面的列表等数据,那么您可以组装一个 View 模型,其属性包括:Project,NumberInProgressPrjects,OverdueProjectsList等。
public class ProjectDashboardViewModel
{
public Project Project { get; set; }
public int NumberInProgressProjects { get; set; }
public ICollection<OverdueProjectInfo> OverdueProjectsList { get; set; }
}

仅仅是一个例子,关键是您可以使用 View 模型来封装 View 所需的所有数据,而不是 Controller 返回与单个域对象(通常是数据库中的表)相匹配的模型对象,然后使ViewData集合中的其余页面起作用所需的其他数据(例如,填充下拉列表所需的数据)。关于 View 模型的文章很多,例如this previous question covers them exhaustively,在.NET MVC Core中与其他版本的MVC一样重要。

“..我需要放在哪里?”

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

关于c# - ViewModel概念仍然存在于ASP.NET MVC Core中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40827377/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com