javascript - 从数据库中获取数据 (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;
}
解决方案
转到与视图关联的控制器/操作并修改模型。不,如果视图中没有数据,您不能使用 JQUERY 或 JS 对其进行修改。
PS 有可能从后端获取额外的数据,前提是你有一个接口(控制器/动作可以为你提供这种数据)
推荐阅读
- laravel - dataTable ajax URL 在 laravel 5.0 中不起作用
- java - 无法在适配器类中绑定 ImageView
- angular-ui-bootstrap - 如何取消引导覆盖?
- python - aiohttp over pycurl multi,因为python有gil,切换到aiohttp有什么好处?
- postgresql - spring MVC + PostgreSQL之间如何建立数据库连接?
- python - 我的for循环不会遍历python中的字符串
- javascript - 如何验证 mxgraph 中单元格的未连接边
- android - 如何修复 Android 中的代码以解决“找不到符号”?
- flask-wtforms - 如何使用 wtforms 设置复选框标签的类?
- r - 如何使用“data.table”添加缺失的日期并填充组内的值