I have 2 tables, 1 with countries, 1 with states.
The states table has a column with Population.
I’m using entities and I have created a List of states for the countries
public class TblCountries
{
//Entities for my table country
...
public List<tblStates> States { get; set; }
}
So now I can for example List all the states that belong to a country.
Now what I want to do is count the population, so I can show the population that of an entire country.
I tried using in my view
@foreach (var item in Model.Countries) {
@Html.DisplayFor(modelItem => item.States.Count<population>)
}
But this doesn’t work, anyone know how to do this?
Thanks in advanced!
You need to use the Linq Sum Function
I haven’t checked this code, but it would be something like this:
Dmitriy’s opinion about keeping the logic in the middle-tier is sound advice; however, those lines are blurring with .NET MVC and the view model pattern (which causes much grumbling among the purists out there).
My recommendation is that if you can isolate your logic in a class behind your controller, then do so. If that is not possible for a given view then feel free to add logic to your view. That, after all, is what the syntax is for.
An example of a time that you may need to do this is if you have a generic view model delivered to many views but need a few specific logic tweaks in certain views.
Good luck!