我设置了一个控制器,该控制器根据从下拉列表传递的值过滤结果,并提供一个搜索字词,然后更新表。该表还通过表头上的链接进行了排序(asc / desc)。我想要做的是让用户搜索特定术语,然后对该数据集进行排序。现在单击标题时,它将在搜索过滤器之前对原始数据集进行排序。如何在请求时保留原始两个参数searchString
和field
的值并将其添加到sortOrder
以便对该数据集进行排序?
public async Task<IActionResult> DisplayViews(String sortOrder, string searchString, string field)
{
ViewData["NameSortParm"] = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
ViewData["DefinitionSortParm"] = sortOrder == "Definition" ? "Definition_desc" : "Definition";
ViewData["OwnerSortParm"] = sortOrder == "Owner" ? "Owner_desc" : "Owner";
ViewData["CurrentFilter"] = searchString;
var Tables = _context.Objects.Where(Object => Object.Type == "View");
switch (sortOrder)
{
case "name_desc":
Tables = Tables.OrderByDescending(s => s.Name);
break;
case "Definition":
Tables = Tables.OrderBy(s => s.Definition);
break;
case "Definition_desc":
Tables = Tables.OrderByDescending(s => s.Definition);
break;
case "Owner":
Tables = Tables.OrderBy(s => s.Owner);
break;
case "Owner_desc":
Tables = Tables.OrderByDescending(s => s.Owner);
break;
default:
Tables = Tables.OrderBy(s => s.Name);
break;
}
if (!String.IsNullOrEmpty(searchString))
{
if((!String.IsNullOrEmpty(field) || !String.IsNullOrWhiteSpace(field)) && field == "Name")
{
Tables = Tables.Where(s => s.Name.Contains(searchString));
}
else if((!String.IsNullOrEmpty(field) || !String.IsNullOrWhiteSpace(field)) && field == "Definition")
{
Tables = Tables.Where(s => s.Definition.Contains(searchString));
}
else if ((!String.IsNullOrEmpty(field) || !String.IsNullOrWhiteSpace(field)) && field == "Owner")
{
Tables = Tables.Where(s => s.Owner.Contains(searchString));
}
//return View(await Tables.AsNoTracking().ToListAsync());
}
return View(await Tables.AsNoTracking().ToListAsync());
}