首页 > 解决方案 > 在 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;
            }
        }
    }

我需要您的帮助,了解如何为每个文件返回计数器,如何为每个文件调用函数并将其呈现在数据表中。

标签: c#jsonajaxasp.net-mvcasp.net-core

解决方案


推荐阅读