我想过滤数据库中的条目,然后对过滤的结果应用升序/降序排序。但是,我不知道该怎么做!我可以过滤或排序,但是如果我过滤然后应用排序,则过滤器将被清除。
我的控制器:
public async Task<IActionResult> Index(string sortOrder, string searchString, string cityString, string specialtyString)
{
ViewData["NameSortParm"] = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
ViewData["nameFilter"] = searchString;
ViewData["cityFilter"] = cityString;
ViewData["specialtyFilter"] = specialtyString;
var providers = from p in _context.Provider
select p;
if (!String.IsNullOrEmpty(searchString))
{
providers = providers.Where(s => s.ProviderName.Contains(searchString));
}
if (!String.IsNullOrEmpty(cityString))
{
providers = providers.Where(s => s.City.Contains(cityString));
}
if (!String.IsNullOrEmpty(specialtyString))
{
providers = providers.Where(s => s.ProviderType.Contains(specialtyString));
}
switch (sortOrder)
{
case "name_desc":
providers = providers.OrderByDescending(m => m.ProviderName);
break;
default:
providers = providers.OrderBy(m => m.ProviderName);
break;
}
return View(await providers.AsNoTracking().ToListAsync());
}
-和相应的视图(局部):
<th>
<a asp-action="Index" asp-route-sortOrder="@ViewData["NameSortParm"]">@Html.DisplayNameFor(model => model.ProviderName)</a>
</th>
我希望过滤器参数能够持续存在,直到用户单击辅助功能以清除它们为止。
感谢您的见解和建议!