HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Stock list view

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
liststockview

Problem

I feel like using this many partial views where a view renders a partial that renders a partial is just digging a hole of poor design, so I'm looking for any suggestions or guidance as to if this is best/worst practice. One of the reasons I don't like it is because it seems too easy to duplicate code across the different views to handle styling. Here they are in order of nesting.

The index view:

@model IEnumerable

@{
ViewBag.Title = "Index";
}

Demo Stock Watch List

StockQuote

    

    

    
        
        
    

@Html.Partial("_stocks", Model)


The _stocks view:

@model IEnumerable

    
        
            Symbol
            Last Price
            Change
            Change Percent
            Volume
            
        
            @Html.Partial("stockList", Model)


The stockList view:

@model IEnumerable

@foreach (var item in Model)
{
string cssClass;
if (item.Change > 0)
{
    cssClass = "num-pos";
}
else if (item.Change 
    @item.Symbol
    @item.LastPrice
    @Html.DisplayFor(modelItem => item.Change)
    @Html.DisplayFor(modelItem => item.ChangePercent)
    @item.Volume
    
         
    

}


And finally the stock partial which is used as the response to an Ajax call to insert a single row into the table:

@model RESTDemo.Models.StockQuote

@{
string cssClass;

if (Model.Change > 0) {
    cssClass = "num-pos";
}
else if (Model.Change 
@Model.Symbol
@Model.LastPrice
@Html.DisplayFor(modelItem => Model.Change)
@Html.DisplayFor(modelItem => Model.ChangePercent)
@Model.Volume

     

Solution

First of all, I don't see anything you have done here as bad practice or poor design, that being said, I would probably not opt to nest as deeply. In my thinking, breaking up the the view into sub views should be done for 2 possible reasons.

  • Organization



  • Reusibility



I am assuming that you don't intend to reuse these views elsewhere in the application, if that is the case, you are only gaining organization. When that is the case, I would probably not put the list body into a separate view, but only the list elements. In other words, break into a subview only when your bound context changes, IE, you are binding to a item instead of the collection.

Context

StackExchange Code Review Q#40204, answer score: 3

Revisions (0)

No revisions yet.