tags:

views:

36

answers:

2

I would like to append to the first line '.OrderBy' and specify a field called 'OrderById'. I'm finding it confusing, i.e. Func & KeySelector etc, i've looked up some examples but not really getting anywhere, cannot find an example that suits what i'm trying to do.

        <% foreach (var item in Model.PaymentItemTotals)
        { %>
            <tr>
                <td><%= Html.Encode(item.Line)%></td>
                <td><strong><%= String.Format("{0:C}", item.AmountTotal)%></strong></td>
                <td><%= String.Format("{0:C}", item.Amount)%></td>
                <td><%= String.Format("{0:C}", item.AmountIndex)%></td>
            </tr>


         <% } %>

Many thanks in Advance

John

A: 
foreach (var item in Model.PaymentItemTotals
    .OrderBy(paymentItemTotal => paymentItemTotal.OrderById))

But consider doing this ordering either in your Model or Controller to make your view as stupid as possible.

Yuriy Faktorovich
Thanks this resolve my issue! :-)
John
Not sure I agree. I would consider display order to be a view-level decision, not Model or Controller level.
Robaticus
@Robaticus: It is a hard call, but in this case I like to think of it in a different way. If there was another View, maybe using the new binding style instead of the old ASP.NET way, or just simply a View for a phone, they would all have to sort the data. But if you pass in the IOrderedEnumerable, all the Views would just have to display it.
Yuriy Faktorovich
@Yuriy, I can see it both ways. Thanks for your insight on this.
Robaticus
A: 
 collection.OrderBy(o => o.FieldToOrderBy)

You may order on the controller before you pass it to the view.

Esteban Araya