【问题标题】:Getting data from database (mssql)从数据库中获取数据 (mssql)
【发布时间】: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


【解决方案1】:

转到与视图关联的控制器/操作并修改模型。不,如果视图中没有数据,则不能使用 JQUERY 或 JS 对其进行修改。

附:有可能从后端获取额外的数据,前提是你有一个接口(控制器/动作可以为你提供这种数据)

【讨论】:

  • 所以我要修改模型。问题是视图中有数据,但查询每页只有 4 篇文章,并且过滤器只对它们进行操作。我已经用控制器代码更新了问题,该代码获得了固定数量的文章。
猜你喜欢
  • 2013-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-06
  • 1970-01-01
相关资源
最近更新 更多