-1

I am not sure this is an error in my code or maybe something else messing with it however I have a function were I am creating a simple list from a MVC model in an ActionResult I have others that use OrderByDescending and work but this is throwing an error for some reason.

[Authorize(Roles = "Administrator, Manager, User")]
[SessionExpire]
public ActionResult PrepareShipment(int id,string sortOrder)
{
    //ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
    //ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";

    Order orderResult = (from o in db.Orders.Include("OrderItems").Include("OrderItems.Item") 
                         where o.OrderID == id 
                         select o).First();
    orderResult = orderResult.OrderByDescending(o => o.Item.ItemName);

    return View(orderResult);
}

That is my complete ActionResult the database has a table Order with another object called Item that object has a column called ItemName. I already have a view with a list in it done exclusively in the view but now I have to order that data by the item name. This is so every time an action is done on the page the list stays in the same order as when it first loaded. Right now every time I reload the page the list reorders.

ekad
  • 14,436
  • 26
  • 44
  • 46
Damion
  • 1
  • Notes from comments on my now-deleted answer: the OP wants to sort the items, not the order itself. This sequence of items is being iterated over in the view, which we don't have the code for... I've suggested that the OP use `OrderByDescending` in the view (on the sequence of items) but the OP hasn't been able to use this advice yet, and we can't provide sample code as we don't know what the view code looks like... – Jon Skeet Dec 23 '14 at 23:01
  • sorry about not giving you all the information you needed I am new to asking questions and I normally can find any answer I am looking for. – Damion Dec 23 '14 at 23:18

1 Answers1

0

Ok so I solved this in the foreach which I realize now I should have included in the original post

the orginal for each was this

@foreach (OrderItem item in Model.OrderItems) {

I fixed it by doing this to the foreach

@foreach (OrderItem item in Model.OrderItems.OrderBy(m => m.OrderItemID)) {

I never even knew you could add additional information like that to a foreach in c#

Thank you everyone for you patience with me :D

Damion
  • 1