anonymous
anonymous

Reputation: 85

ASP.net MVC Table Grouping

I need some help with grouping my data. I have the following bit below, where data is displayed based on a foreach loop.

Very straight forward stuff, but I need to group it by item.PARTY. There are ever only two and when you click on one it expands ( I can use the bootstrap bit for that). Its just the grouping I'm a bit confused and not even sure where to start and what is best practise.

 @foreach (var item in Model.FinplanExpenseView)
    {
        <tr>
            <td class="text-center">
                @if (item.VIEW_TYPE == "Budget")
                {                   
                     @Html.ActionLink("X", "Delete", item.VIEW_TYPE, new { id = item.FINPLAN2_GID, ReturnAction = "IncomeAndExpenses" }, null)
                }
                else if (item.VIEW_TYPE == "LivingExpenses")
                {
                    @Html.ActionLink("X", "Delete", item.VIEW_TYPE, new { id = item.FINPLAN2_GID, party = item.PARTY, ReturnAction = "IncomeAndExpenses" }, null)                   
                }
                else
                { 
                    @Html.ActionLink("X", "Delete", item.VIEW_TYPE, new { id = item.SOURCE_GID, ReturnAction = "IncomeAndExpenses" }, null)                   
                }
            </td>
            <td>
                @if (item.VIEW_TYPE == "Budget")
                {                   
                    @item.DESC
                }
                else if (item.VIEW_TYPE == "LivingExpenses")
                {                   
                    @item.DESC
                }
                else
                {                   
                    @item.DESC
                }

            </td>           
            <td class="numeric-column">R @Html.DisplayFor(modelItem => item.AMOUNT)</td>
            <td class="text-right">@Html.DisplayFor(modelItem => item.PARTY)</td>
            <td class="text-right">                
                @if (item.VIEW_TYPE == "Budget")
                {                   
                     @Html.ActionLink("edit", "Edit", item.VIEW_TYPE, new { id = item.FINPLAN2_GID, ReturnAction = "IncomeAndExpenses" }, null)
                }
                else if (item.VIEW_TYPE == "LivingExpenses")
                {
                    @Html.ActionLink("edit", "Edit", item.VIEW_TYPE, new { id = item.FINPLAN2_GID, party = item.PARTY, ReturnAction = "IncomeAndExpenses" }, null)                   
                }
                else
                { 
                    @Html.ActionLink("edit", "Edit", item.VIEW_TYPE, new { id = item.SOURCE_GID, ReturnAction = "IncomeAndExpenses" }, null)                   
                }
            </td>
        </tr>
    }

Any help would be appreciated!

Upvotes: 2

Views: 2492

Answers (1)

LockTar
LockTar

Reputation: 5465

You can use an orderby for this. You want all the Party items after each other right?

Change this: @foreach (var item in Model.FinplanExpenseView)

in

@foreach (var item in Model.FinplanExpenseView.Orderby(f => f.PARTY)

Upvotes: 1

Related Questions