首页 > 解决方案 > 从数据库中获取数据 (mssql)

问题描述

该项目是用 .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;
}

标签: javascriptc#asp.net-mvcrazorssms

解决方案


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

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


推荐阅读