mhd
mhd

Reputation: 444

Should I split my Views up into a lot of small Views or just make it one big View?

I am currently working on an MVC 3 Web app project where I need to display a lot of information on the front page. I am relatively new at web page layout and design so I have hit a wall in terms of how to structure the code behind for this page.

My first thoughts were to split the page (View) up into as many pages (small Views) as possible so the View Models are smaller and easier to manage. Although I am not sure if this is the correct way of going about it. Or should I try to fit all the needs of the page into one view that will display all the information I need?

Upvotes: 2

Views: 3844

Answers (3)

James Lawruk
James Lawruk

Reputation: 31337

Yes, splitting up a large view into partial views can be positive even if you know for certain the partial views will not be reused. If your large view is conceptually divided into sections, then it makes sense to split up the view accordingly. These sections may not be apparent in a large view file with tons of nested div tags. Of course, find the right balance. A partial view for every div tag would be overkill. :)

@{
    Layout = "~/Views/Shared/_Layout.cshtml"
}

<div id="page">
    @Html.Partial("Top")
    @Html.Partial("Middle")    
    @Html.Partial("Bottom")        
</div>

Upvotes: 2

rae1
rae1

Reputation: 6134

You will get a number of benefits from splitting a large view object into smaller views, primarily reusability, testability and the fact you won't have to do it (inevitably) two months from now.

Using smaller views will first give you the ability to reuse those views in other parts of the project (there should be an "if necessary", but the chances are fairly high that you will). Using a single view gives you no such flexibility.

Smaller views will also be simpler to conceptualize (a concise, properly-named view will save you the time to read through its one-hundred lines), to maintain (you can isolate bugs, and feature changes to each view without affecting others) and to test (in time, complexity and dependency injection).

You do not gain anything by using a single, humongous view (well, perhaps a couple of minutes of development), yet what you miss in the long (and short) period, is substantial.

To use several smaller views in MVC3 you can use partial views inside the main view,

<div>
  @Html.Partial("_PartialView", new PartialViewModel)
</div>

Upvotes: 2

M.Ob
M.Ob

Reputation: 1837

This gets the good old answer of "it depends" :)

If different parts of the front page could be used in other places, it would make sense to break it up into several logical partial views. But, if not, there is no reason to break it all up. You should simply construct a nicely designed view model.

Short of knowing more details, maybe something like this very crude example:

public class FrontPageViewModel
{
    public Section1Model section1 { get; set; }
    public Section2Model section2 { get; set; }
    public Section3Model section3 { get; set; }
    public Section4Model section4 { get; set; }
}

... where your section models are built with the logical properties

public class Section1Model
{
    public string Property1 { get; set; }
    public string Property2 { get; set; }
    public string Property3 { get; set; }
}

public class Section2Model
{
    public string Property1 { get; set; }
    public string Property2 { get; set; }
    public string Property3 { get; set; }
    public string Property4 { get; set; }
    public string Property5 { get; set; }
}
// etc.

Then in your view you could do this:

<div id="section1Content">
    @Html.DisplayFor(m => m.section1)
</div>
<div id="section2Content">
    @Html.DisplayFor(m => m.section2)
</div>
<!-- etc. -->

Upvotes: 1

Related Questions