Liam
Liam

Reputation: 455

Problems sorting a list of data in ASP.net MVC3

I am trying to implemeny the sorting functionality thats shown in the Entity Framework tutorials on the the Asp.net MVC Site, here

http://www.asp.net/entity-framework/tutorials/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application

For some reason although i can see that the correct query strings are being added to the url, the data is not sorting,

I have the following switch statement in my controller to allow me to sort the data by the customer name or the primary contact name

//Returns a list of all the customers to be displayed on the (Master edit page)
    //- TODO - Implement Paging Functionality
    public ViewResult Index(string sortOrder)
    {
        ViewBag.CustomerNameSortParm = String.IsNullOrEmpty(sortOrder) ? "CustomerName desc" : "";
        ViewBag.PrimaryContactNameSortParm = sortOrder == "PrimaryContactName" ? "PrimaryContactName desc" : "PrimaryContactName";
        var cust = repository.Customers;

        switch (sortOrder)
        {
            case "CustomerName desc":
                cust = repository.Customers.OrderByDescending(s => s.CustomerName);
                break;
            case "PrimaryContactName":
                cust = repository.Customers.OrderBy(s => s.PrimaryContactName);
                break;
            case "PrimaryContactName desc":
                cust = repository.Customers.OrderByDescending(s => s.PrimaryContactName);
                break;
            default:
                cust = repository.Customers.OrderBy(s => s.CustomerName);
                break;
        }

        return View(repository.Customers.ToList());
    }

Then in the view i have links created for each of the coumn headings

<th class="header">
       @Html.ActionLink("Customer Name", "Index", new { sortOrder=ViewBag.CustomerNameSortParm })
    </th>
    <th class="header">
      @Html.ActionLink("Contact Name", "Index", new { sortOrder=ViewBag.PrimaryContactNameSortParm })
    </th>

When i click on the links i can see the query string values in the URL like

http://localhost:58783/Admin/Index?sortOrder=PrimaryContactName

But the data is not being sorted, has anyone experienced similar issues, or know where i am going wrong? for some reason the comments at the bottom of the Entity Framework tutorial are not being displayed so i cant see if anyone else has has this issue.

The only difference i can see with my code is that in the tutorial a LINQ query is used in the controller, where i am using the repository and returning my customers from the repository which returns an IQueryable list of customers.

Any advice is appreciated.

Liam.

Upvotes: 1

Views: 667

Answers (1)

ipr101
ipr101

Reputation: 24236

I think your return statement is wrong, it should be -

return View(cust.ToList());

rather than -

return View(repository.Customers.ToList());

You appear to be sorting the data in to the 'cust' variable but then returning a non-sorted collection to your view.

Upvotes: 2

Related Questions