Best Practice for Summary Footer (and the like) in MVC

Posted by benpage on Stack Overflow See other posts from Stack Overflow or by benpage
Published on 2010-03-12T01:49:38Z Indexed on 2010/03/12 1:57 UTC
Read the original article Hit count: 237

Filed under:
|

Simple question on best practice. Say I have:

public class Product
{
    public string Name { get; set; }
    public string Price { get; set; }
    public int CategoryID { get; set; }
    public bool IsAvailable { get; set; }
}

and i have a view using IEnumerable< Product> as the model, and i iterate through the Products on the page and want to show the total of the prices at the end of the list, should I use:

<%= Model.Sum(x=> x.Price) %>

or should I use some other method? This may extend to include more involved things like:

<%= Model.Where(x=> x.CategoryID == 5 && x.IsAvailable).Sum(x=> x.Price) %>

and even

<% foreach (Product p in Model.Where(x=> x.IsAvailable) {%>
-- insert html --
<% } %>

<% foreach (Product p in Model.Where(x=> !x.IsAvailable) {%>
-- insert html --
<% } %>

I guess this comes down to should I have that sort of code within my view, or should i be passing it to my view in ViewData? Or perhaps some other way?

© Stack Overflow or respective owner

Related posts about subjective

Related posts about asp.net-mvc