c# - 在 asp.net mvc 中使用 AJAX 过滤
问题描述
我有这个数据表,它显示数据库中每个文件的数据和两个数据选择输入 DateFrom 和 DateTo :
<div class="form-group row ">
<label asp-for="DateFrom" class="col-sm-2 col-form-label"></label>
<div class="col-sm-10">
<input type="date" id="datefrom" asp-for="DateFrom" class="form-control" placeholder="DateFrom" />
</div>
</div>
<div class="form-group row ">
<label asp-for="DateTo" class="col-sm-2 col-form-label"></label>
<div class="col-sm-10">
<input type="date" asp-for="DateFrom" class="form-control" placeholder="DateTo" />
</div>
</div>
@if (Model.Files.Count == 0)
{
<caption>No Records Found</caption>
}
else
{
<table id="files" class="table table-striped">
<thead>
<tr>
<th>Thumbnail</th>
<th>Name</th>
<th>Number of downloads</th>
<th>Genre</th>
<th>File Type</th>
<th>Published On</th>
<th>Created On</th>
<th>Author</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach (var file in Model.Files)
{
var thumbPath = "~/Thumbnails/" + file.ThumbnailPath;
<tr>
<td><img style="width:80px;height:80px;" src="@thumbPath" asp-append-version="true" /></td>
<td><a asp-controller="files" asp-action="details" asp-route-id="@file.Id" class="text-dark">@file.Name</a></td>
<td>@file.DownloadCounter</td>
<td>@file.Genre.GenreName</td>
<td>@file.FileT.FileTypeName</td>
<td>@file.PublishedOn</td>
<td>@file.Year</td>
<td>@file.Author</td>
<td>
<a asp-controller="files" asp-action="QrCode" asp-route-id="@file.Id" class="btn btn-primary">Qr Code</a>
<a asp-controller="files" asp-action="EditFile" asp-route-id="@file.Id" class="btn btn-primary">Edit</a>
</td>
</tr>
}
</tbody>
</table>
}
因此,如果 DateFrom 和 DateTo 为空,则不会发生任何事情,但如果它们不为空,我想为每个文件调用此 JSON 函数。此函数应返回给定期间 DateFrom 和 DateTo 的下载次数,因此我想显示该数字而不是 @file .DownloadCounter 在数据表中,这里是 JSON 函数:
public JsonResult NumberDownloads(DateTime dateFrom,DateTime dateTo,FileModel file)
{
var stats = context.Statistic.Where(x => x.File == file).ToList();
foreach(var item in stats)
{
int counter = 0;
int Compare = DateTime.Compare(item.DownloadDate, dateFrom);
int Compare2 = DateTime.Compare(item.DownloadDate, dateTo);
if((Compare == 0) || (Compare2 == 0))
{
counter+=item.DownloadCounter;
}else if ((Compare >= 0) && (Compare2 <= 0))
{
counter+=item.DownloadCounter;
}
else
{
continue;
}
}
}
我需要您的帮助,了解如何为每个文件返回计数器,如何为每个文件调用函数并将其呈现在数据表中。
解决方案
推荐阅读
- rabbitmq - RabbitMQ:basic_ack也需要服务器确认吗?
- node.js - 如何为不同的用户设计模式模型
- excel - 将电子邮件数据导出到工作簿时如何检查数据是否已经在工作表中?
- flutter - 使用 Flutter 启用移动数据
- if-statement - Excel 编码:如何对数据进行分类并达到 3 个可能输出中的 1 个
- asp.net - AntiForgeryToken 应该在循环内部还是外部?
- php - 如何使用functions.php更改wordpress页面中图像的属性(标题、标题)?
- sql - 表格中的累计总数
- html - 有没有办法防止使用 step 属性验证输入字段?
- makefile - Makefile 非递归方法问题