18

In previous versions of ASP.NET MVC you find some informations about ViewModels and how to use them in this version.

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.

Models:

    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; }
        ...
    }

Before ASP.NET Core I created a ProjectDashboardViewModel for getting information to the view. Can I use the same approach?

tomRedox
  • 28,092
  • 24
  • 117
  • 154
itcompi
  • 220
  • 1
  • 3
  • 8

3 Answers3

32

"Does the concept still exist?" "Can I use the same approach?"

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.

"I can't find any information about this topic in ASP.NET Core MVC"

The official documentation discusses view models extensively. The Overview of ASP.NET Core MVC section has this to say:

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 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.

A search through the docs as follows highlights some more too: https://learn.microsoft.com/en-us/search/index?search=viewmodel&scope=ASP.NET+Core

"..i want to make a dashboard for projects"

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.

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; }
}

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.

"..where i need to put them?"

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'.

tomRedox
  • 28,092
  • 24
  • 117
  • 154
3

ViewModel / MVVM (Model-View-ViewModel) is an architectural pattern and not tied to any framework or stack.

Means you can still use it, it is just an additional abstraction layer on top of the MVC pattern which brings the data in a form that makes it easy to consume for the view.

Tseng
  • 61,549
  • 15
  • 193
  • 205
  • Generelly i know that's a architectural pattern but i'm wondering why i can't find any information in asp.net core about MVVM but in other variants of asp.net mvc... That's why i ask you. **Please not down vote my questions! First answer the whole question.** – itcompi Nov 27 '16 at 17:49
  • There is no particular reason. Most of the tutorials or articles either cover whats new (configuration, new features) or basic tutorials (were MVC with VM would be overengineered for an simple example). And VM with MVC obviously isn't anything new. The official ASP.NET Core sample project (MusicStore) still use it https://github.com/aspnet/MusicStore/tree/rel/1.1.0/samples/MusicStore/ViewModels – Tseng Nov 27 '16 at 18:07
  • Also how do you expect us to tell you why bloggers don't post about it when writing the samples, its up to the author of such sources to add it or leave it out – Tseng Nov 27 '16 at 18:08
-1

You can use the relationsships from the dbcontext https://learn.microsoft.com/en-us/ef/core/modeling/relationships

demser
  • 1