问题描述
在以前的 ASP.NET MVC 版本中,您可以找到一些关于 ViewModel 以及如何在此版本中使用它们的信息.
In previous versions of ASP.NET MVC you find some informations about ViewModels and how to use them in this version.
我想知道为什么我在 ASP.NET Core MVC 中找不到有关此主题的任何信息?这个概念是否仍然存在,如果存在,我需要将它们放在哪里?
I'm wondering why I can't find any information about this topic in ASP.NET Core MVC? Does the concept still exist and if so where i need to put them?
问题出现是因为我想为项目制作仪表板.项目是我的网络应用程序的主要入口点.他们有很多关系,例如里程碑.
The question comes up because i want to make a dashboard for projects. Projects are the main entry point in my web app. They have many relationships e.g with milestones.
型号:
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 来获取视图信息.我可以使用相同的方法吗?
Before ASP.NET Core I created a ProjectDashboardViewModel for getting information to the view. Can I use the same approach?
推荐答案
这个概念还存在吗?"我可以使用相同的方法吗?"
是的,ViewModel 概念仍然适用于 .NET Core,您仍然可以像以前一样使用它们,即将选择的数据组合成符合特定视图需求的形状".
Yes, the ViewModel concept is still applicable in .NET Core and you would still use them as before, i.e. to assemble a selection of data into a 'shape' that matches the needs of a particular view.
我在 ASP.NET Core MVC 中找不到有关此主题的任何信息"
官方文档广泛讨论了视图模型.ASP.NET Core 概述MVC 部分 有这样的说法:
The official documentation discusses view models extensively. The Overview of ASP.NET Core MVC section has this to say:
模型职责
MVC 应用程序中的模型表示应用程序和任何业务逻辑或操作应该是由它执行.业务逻辑应该封装在模型中,以及用于持久化状态的任何实现逻辑应用.强类型视图通常使用 ViewModel 类型专门设计用于包含要在该视图上显示的数据;这控制器将从型号.
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.
在呈现 HTML带视图部分:
您可以使用多种机制将数据传递给视图.最健壮的方法是在视图中指定模型类型(通常称为作为视图模型,以将其与业务域模型类型区分开来),然后将这种类型的实例从动作传递给视图.我们建议您使用模型或视图模型将数据传递给视图.
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.
MVC/高级/应用程序部件部分 还讨论了视图模型,那里的示例代码展示了如何将许多不同的对象组装在一起,以供视图与视图模型一起使用.
The MVC/Advanced/Application Parts section also discusses View Models, the sample code there shows how you can assemble a number of different objects together for consumption by the view with a view model.
他们还提到了他们在部分视图部分一个>.有一些示例代码与 这里,但这些示例实际上并没有真正突出模型和视图模型之间的区别.
They also mention them in the section on Partial Views. There is some sample code that goes along with that here, but those examples don't actually really highlight the difference between a model and a view model.
通过以下文档搜索也可以突出显示更多内容:https://docs.microsoft.com/en-us/search/index?search=viewmodel&scope=ASP.NET+Core
A search through the docs as follows highlights some more too: https://docs.microsoft.com/en-us/search/index?search=viewmodel&scope=ASP.NET+Core
..我想为项目制作仪表板"
在您的情况下,您提供的数据仅显示一个具有一些子对象的域对象(项目").如果这就是您想要显示的所有数据,那么您可能不需要视图模型,因为它只是您的 Project
模型的镜像.
In your case the data you've provided just shows a single domain object (the 'Project') which has some child objects. If that's all the data you want to show then you probably don't need a view model as it would simply be a mirror of your Project
model.
但是,如果您想在项目仪表板上显示其他信息,例如一些数据汇总了有关正在进行的项目数量的数据、落后的项目列表等.然后您可以组装一个具有以下属性的视图模型:Project、NumberInProgressPrjects、OverdueProjectsList 等.
However, if you want to show other info on the Project dashboard, e.g. some data aggregated data about the number of projects in progress, a list of which projects are behind etc. then you might assemble a view model with properties for: Project, NumberInProgressPrjects, OverdueProjectsList etc.
public class ProjectDashboardViewModel
{
public Project Project { get; set; }
public int NumberInProgressProjects { get; set; }
public ICollection<OverdueProjectInfo> OverdueProjectsList { get; set; }
}
这只是一个示例,重点是您可以使用视图模型来封装视图所需的所有数据,而不是控制器返回与单个域对象(通常是数据库中的表)匹配的模型对象然后是使 ViewData
集合中的其余页面功能所需的大量附加数据(例如,填充下拉列表所需的数据).有很多关于视图模型的优秀文章,上一个问题详尽地介绍了它们,例如,并且在 .NET MVC Core 中与其他版本的 MVC 一样相关.
That's just an example, the point is you can use the view model to encapsulate all of the data needed by your view, rather than your controller returning a model object that matches a single domain object (often a table from your database) and then lots of additional data that's needed to make the rest of the page function in the ViewData
collection (e.g. the data needed to populate the drop down lists). There are many excellent articles on view models, this previous question covers them exhaustively for example, and is just as relevant in .NET MVC Core as other versions of MVC.
..我需要把它们放在哪里?"
您可以将它们放在您选择的位置,只要确保在需要时使用 using
语句即可.小型项目的典型惯例是将它们放在一个名为ViewModels"的文件夹中.
You can put them where you choose, just make sure you use a using
statement if needed. The typical convention in smaller projects is to put them in a folder called 'ViewModels'.
这篇关于ASP.NET MVC Core 中还存在 ViewModel 概念吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!