Dolly93
Dolly93

Reputation: 46

Can't sort 3 columns in MVC 4 - ASP.net

Normal sorting works if I just implement the sorting on 2 columns, in this example "Bedrag" & "Datumbetaling".

When I want to add sorting on a third column called "StatusBetalingID" then It only sorts Descending if I click on the column, if I click on this column a second time, It wouldn't sort ascending. When I keep clicking on the first 2 columns "Bedrag" & "DatumBetaling" then It keeps sorting ascending & descending.

Here is my code in the controller:

public ActionResult Index(string sortBy)
{
    ViewBag.SortBedragParameter = string.IsNullOrEmpty(sortBy) ? "Bedrag_desc" : "";
    ViewBag.SortDatumParameter = sortBy == "DatumBetaling" ? "DatumBetaling_desc" : "DatumBetaling";
    ViewBag.SortStatusParameter = sortBy == "StatusBetaling" ? "stat" : "Status_desc";
    var betalingen = betalingBLL.GetAll();
    switch (sortBy)
    {
       case "Bedrag_desc":
                betalingen = betalingen.OrderByDescending(x => x.Bedrag);
                break;
       case "DatumBetaling_desc":
                betalingen = betalingen.OrderByDescending(x => x.DatumBetaling);
                break;
       case "DatumBetaling":
                betalingen = betalingen.OrderBy(x => x.DatumBetaling);
                break;
        case "Status_desc":
                betalingen = betalingen.OrderByDescending(x => x.StatusBetalingID);
                break;
        case "stat":
                betalingen = betalingen.OrderBy(x => x.StatusBetalingID);
                break;
        default:
                betalingen = betalingen.OrderBy(x => x.Bedrag);
                break;
     }
     return View(betalingen.ToList());
}

Here is the code in the view:

<th> <%: Html.ActionLink("Status Betaling", "Index", new { sortBy = ViewBag.SortStatusParameter}) %> </th>
<th> <%:Html.ActionLink("Datum Betaling", "Index", new { sortBy =  ViewBag.SortDatumParameter}) %> </th>
<th> <%:Html.ActionLink("Bedrag", "Index", new { sortBy = ViewBag.SortBedragParameter }) %> </th>

So it actually works, the sorting, but I don't know how to solve the problem with the third parameter/column to keep sorting this one.

Upvotes: 1

Views: 848

Answers (1)

Kenneth
Kenneth

Reputation: 28737

The reason is that sortby will never equal StatusBetaling (why would it) and thus this line will always be set to Status_desc:

ViewBag.SortStatusParameter = sortBy == "StatusBetaling" ? "stat" : "Status_desc";

The following code should do it:

ViewBag.SortBedragParameter = string.IsNullOrEmpty(sortBy) ? "Bedrag_desc" : "";
    ViewBag.SortDatumParameter = sortBy == "DatumBetaling" ? "DatumBetaling_desc" : "DatumBetaling";
    ViewBag.SortStatusParameter = sortBy == "StatusBetaling" ? "Status_desc" : "StatusBetaling";
    var betalingen = betalingBLL.GetAll();
    switch (sortBy)
    {
       case "Bedrag_desc":
                betalingen = betalingen.OrderByDescending(x => x.Bedrag);
                break;
       case "DatumBetaling_desc":
                betalingen = betalingen.OrderByDescending(x => x.DatumBetaling);
                break;
       case "DatumBetaling":
                betalingen = betalingen.OrderBy(x => x.DatumBetaling);
                break;
        case "Status_desc":
                betalingen = betalingen.OrderByDescending(x => x.StatusBetalingID);
                break;
        case "StatusBetaling":
                betalingen = betalingen.OrderBy(x => x.StatusBetalingID);
                break;
        default:
                betalingen = betalingen.OrderBy(x => x.Bedrag);
                break;
     }
     return View(betalingen.ToList());

Upvotes: 2

Related Questions