【发布时间】:2021-08-20 01:51:13
【问题描述】:
我有一个向用户显示的报告列表。我有允许以不同方式对列表进行排序的 a-tags(最后发布、最高赞成票等),并且我还有单独的 a-tags 可以根据特定报告变量过滤列表(报告状态为打开,关闭等)。
我正在想办法让过滤器在按下用于排序列表的 a-tag 时保持原位,反之亦然。
我尝试的是使用 statusId(设置为过滤列表的 ID)和 sortString(包含 “highest_award”的字符串设置模型em> 或 "last_update", 用于在 HomeController 中对列表进行排序),但我的想法完全错误。
有没有干净的方法来解决这个问题?
Index.cshtml
//List of sorting options -- *These are links that allow the user to sort the list as appropriate*
<ul class="nav nav-pills flex-column">
<li><a asp-area="" asp-controller="Home" asp-action="Index" asp-route-sortString="@ViewData["AwardSort"]">Highest Awarded</a></li>
<li><a asp-area="" asp-controller="Home" asp-action="Index" asp-route-sortString="@ViewData["UpdateSort"]">Last Updated</a></li>
</ul>
...
//Filtering options set to status.Id -- *These are buttons that allow the user to filter the list as appropriate according to a set statusId for each article*
@foreach (ReportStatus status in Model.Statuses)
{
<li class="nav-item fs-5">
<a class="nav-link" asp-area="" asp-controller="Home" asp-action="Index" asp-route-statusId="@status.Id">@status.StatusName</a>
</li>
}
...
//Report contents as viewed by used
@{
foreach (ReportViewModel report in Model.Reports.ReportViewModels)
{
...some "report" details
}
HomeController.cs (没有提到过滤列表)
public IActionResult Index(string sortString/*, int statusId*/)
{
Console.Write(sortString + " " + statusId + "\n");
var reports = from r in _myRepository.GetAllReports()
select r;
ViewData["AwardSort"] = String.IsNullOrEmpty(sortString) ? "highest_award" : "";
ViewData["UpdateSort"] = String.IsNullOrEmpty(sortString) ? "last_update" : "";
//reports = reports.Where(r => r.StatusId == statusId); -- Need to set this and keep set
switch (sortString)
{
case "last_update":
reports = reports.OrderBy(r => r.DateOfUpdate);
break;
case "highest_award":
reports = reports.OrderBy(r => r.NumberOfStars);
break;
}
HallOfFameViewModel hofViewModel = new HallOfFameViewModel(_myRepository.GetTopUsers(5));
ReportListViewModel reportsViewModel = new ReportListViewModel(
reports.ToList(),
_myRepository.GetUserById(_userManager.GetUserId(User))
);
var model = new HomePageViewModel(hofViewModel, reportsViewModel, _myRepository.GetReportStatuses());
return View(model);
}
//-------------------------------------------
//The filtered list would be taken here, need to somehow add currently selected sort option as well as be able to set the sorting criteria
[ResponseCache(Duration = 2)]
[Route("Home/Index/{id}")]
public IActionResult Index(int statusId)
{
var reports = from r in _nemesysRepository.GetAllReports()
select r;
HallOfFameViewModel hofViewModel = new HallOfFameViewModel(_nemesysRepository.GetTopUsers(10));
ReportListViewModel reportsViewModel = new ReportListViewModel(
_nemesysRepository.GetAllReportsWithStatus(statusId).ToList(),
_nemesysRepository.GetUserById(_userManager.GetUserId(User))
);
var model = new HomePageViewModel(hofViewModel, reportsViewModel, _nemesysRepository.GetReportStatuses());
return View(model);
}
提前谢谢你。
【问题讨论】:
-
嗨,有趣,也许将排序和过滤器合并到一个模型中
-
@jspcal 我已经尝试过某种程度的东西,但在某些时候它变得完全令人费解(对 asp-net 来说非常新,几乎不知道它实际上是 3 天前)并完全放弃了它。我尝试在网上找到一些示例进行比较,但找不到任何有用的东西。
标签: html asp.net-mvc asp.net-core model