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>