Big giant view model for long scrolling page

445 views Asked by At

Building an ASP .Net app where the pages are vertically long with several sections. I'm trying to figure out how to structure this, specifically in relation to the view model. For example, my long page has some straight content text, a couple of featured events, a menu where items are stored in the database, etc... Should I have one giant view model, for ex:

public class MyGiantViewModel
{
    public Guid Id { get;set; }
    public IEnumerable<MenuItem> MenuItems { get;set; }
    public string HeaderContent { get;set; }
    public IEnumerable<Event> Events { get;set; }
}

Or should I be breaking this up into smaller components, like partial views with ChildAction controller actions. I am concerned with smaller action of having a whole bunch of database calls, where as with a big view model I can make a single call to get all my data.

1

There are 1 answers

0
beautifulcoder On

I wouldn't worry too much about performance at this point. If you have a giant view model, you can still break the view into partials, for example:

@model MyGiantViewModel
@Html.Partial("_MyPartial", Model.MenuItems)

// In your _MyPartial
@model IEnumerable<MenuItem>

The cool idea here is you get all the data you need at render.