【发布时间】:2021-07-07 13:05:10
【问题描述】:
该项目是用 .NET MVC 技术编写的。这是一个特定页面,该页面显示具有特定类别的文章。基于类别,我创建了过滤器,它使用 JavaScript / jQuery 根据每篇文章添加的类来过滤相关文章。问题是已经在后台编写了代码,每页获取一定数量的文章,并且过滤器仅过滤页面上显示的那些,而不是全部。能否在 JS 的帮助下以某种方式解决这个问题,或者创建某种可以从数据库中获取正确文章的查询?
这段代码显示了如何在视图中添加类别:
<span class="category @foreach (var cat in @item.Category) {@(cat.Title+" ")}">
@foreach (var cat in @item.Category)
{
<span class="cat @cat.Title">@cat.Title</span>
}
</span>
这是获取文章列表的代码:
var list = new List<ArticleViewModel>();
foreach (var catalog in Model.Articles)
{
list.Add(catalog);
}
控制器中用于获取文章的代码:
public async Task<ActionResult> Index(int idContent, string idlang, bool isPreview, string type, int page = 1)
{
ArticleIndexViewModel model = await GetIndexViewModel(idContent, idlang, type, page);
return View(model);
}
private int _numPerPage = 4;
private async Task<ArticleIndexViewModel> GetIndexViewModel(int idContent, string idLang, string type, int page)
{
ArticleIndexViewModel model = new ArticleIndexViewModel();
model.Title = ContentViewModel.FromModel(await RepositoryService.ContentRepository.FindByIdAsync(idContent)).Title;
model.CurrentPage = page;
model.NumPerPage = await RepositoryService.ArticleRepository.CountAsync(p => p.Visible);
model.Articles = (await RepositoryService.ArticleRepository.GetVisible(idLang, (page - 1) * _numPerPage, _numPerPage)).Select(ArticleViewModel.FromModel).ToArray();
model.NumElements = await RepositoryService.ArticleRepository.CountAsync(p=>p.Visible);
model.NumPerPage = _numPerPage;
return model;
}
【问题讨论】:
-
如果我的理解正确,您可能想修改 GetVisible 方法以不返回分页数据。
-
getVisible 不是问题。表列“可见”的值为 0 或 1。基于此,该方法已返回正确的文章。问题在于上面的 jQuery 过滤器和数据库之间的连接。因为过滤器只处理它在单个查询中获得的文章。那是 4,不是全部(200)。
标签: javascript c# asp.net-mvc razor ssms