1

I have a strange problem and I have no idea why it is performing the way it is. I am using ASP.NET with MVC. I have created a sort in my index which works at firsts. However when the column gets to sort ascending it stops working with the exception of the column used as the default value. So let's say I have two columns one is default-ascending and descending sorts, and the other column can sort ascending and descending. I can toggle back and forth between ascending and descending on the first column. However on the second column it will first sort descending and then I can sort ascending and then it will not allow me to sort again. I have no clue as to why.

this is my index in my controller:

public ViewResult Index(string sortOrder, string currentFilter, string searchString, int? page)
    {
        ViewBag.CurrentSort = sortOrder;
        ViewBag.ManSortParm = String.IsNullOrEmpty(sortOrder) ? "man_desc" : "";
        ViewBag.ModelSortParm = String.IsNullOrEmpty(sortOrder) ? "model_desc" : "model_asc";
        ViewBag.ModDescSortParm = String.IsNullOrEmpty(sortOrder) ? "ModDesc_desc" : "ModDesc_asc";

        if (searchString != null)
        {
            page = 1;
        }
        else
        {
            searchString = currentFilter;
        }

        ViewBag.CurrentFilter = searchString;

        var manufacturermodels = db.ManufacturerModels.Include(m => m.Manufacturer);

       // var logTypes = from s in db.DeviceLogTypes
        //               select s;
        if (!String.IsNullOrEmpty(searchString))
        {

            manufacturermodels = manufacturermodels.Where(s => s.Manufacturer.Manufacturer1.Contains(searchString));

        }
        switch (sortOrder)
        {
            case "man_desc":
                manufacturermodels = manufacturermodels.OrderByDescending(s => s.Manufacturer.Manufacturer1);
                break;
            case "model_desc":
                manufacturermodels = manufacturermodels.OrderByDescending(s => s.Model);
                break;
            case "model_asc":
                manufacturermodels = manufacturermodels.OrderBy(s => s.Model);
                break;
            case "ModDesc_desc":
                manufacturermodels = manufacturermodels.OrderByDescending(s => s.ModelDescription);
                break;
            case "ModDesc_asc":
                manufacturermodels = manufacturermodels.OrderBy(s => s.ModelDescription);
                break;
            default:  // Name ascending 
                manufacturermodels = manufacturermodels.OrderBy(s => s.Manufacturer.Manufacturer1);
                break;
        }

        int pageSize = 20;
        int pageNumber = (page ?? 1);
        return View(manufacturermodels.ToPagedList(pageNumber, pageSize));
    }

then in my view I have the following:

@using (Html.BeginForm("Manufacturers and Models", "ManufacturerModel", FormMethod.Get))
{
    <p>
        Find by Manufacturer: @Html.TextBox("SearchString",   ViewBag.CurrentFilter as String)
        <input type="submit" value="Search" />

    </p>
}
<table class="table">
<tr>
    <th>
        @Html.ActionLink("Manufacturer", "Index", new { sortOrder = ViewBag.ManSortParm, currentFilter = ViewBag.CurrentFilter })
    </th>
    <th>
        @Html.ActionLink("Model", "Index", new { sortOrder = ViewBag.ModelSortParm, currentFilter = ViewBag.CurrentFilter })
    </th>
    <th>
        @Html.ActionLink("Description", "Index", new { sortOrder = ViewBag.ModDescSortParm, currentFilter = ViewBag.CurrentFilter })
    </th>
    <th>

    </th>

    <th></th>
</tr>
2
  • what error happend or happen in which part ? Commented Jun 28, 2016 at 19:43
  • as far as I can tell there is no indication of an error Commented Jun 28, 2016 at 20:48

1 Answer 1

1

I finally figured it out. The default works because I am using the correct language and looking for the IsNullOrEmpty

ViewBag.ManSortParm = String.IsNullOrEmpty(sortOrder) ? "man_desc" : "";

While the others are not working because of this same language. What I needed was -

ViewBag.ModelSortParm = sortOrder == "model_asc" ? "model_desc" : "model_asc";
ViewBag.ModDescSortParm = sortOrder == "ModDesc_asc" ? "ModDesc_desc" : "ModDesc_asc";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.