首页 > 解决方案 > 从 AJAX 调用 asp.net mvc 中的视图发送 id

问题描述

我有这个数据表:

<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 id="downloads">@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>
            <td style="display:none;" id="fileId">@file.Id</td>
        </tr>
        
    }
        </tbody>
    </table>
}

我想在这个 ajax 调用中为 Model.Files 中的每个文件发送 file.Id:

<script>
    $(document).ready(function () {
        $('#dateto').change(function () {
            var DateFrom = $('#datefrom').val();
            var DateTo = $('#dateto').val();
            var fileId = $('#fileId').val();
            
                $.ajax({
                    type: 'GET',
                    url: "Home/NumberDownloads/?dateFrom=" + DateFrom + "&dateTo=" + DateTo + "&fileId= " +fileId,
                    success: function (data) {
                        console.log(data);
                    },
                    error: function (error) {
                        console.log(error);
                    }

                });
           
        });
    });
</script>

通过这种方式它不起作用fileId始终为0,其他一切都有效,但我需要你的帮助如何在调用Models.File中的每个文件时发送fileId。

标签: jqueryasp.netajaxasp.net-mvcasp.net-core

解决方案


  <script>
        $(document).ready(function () {
            $('#dateto').change(function () {
                var DateFrom = $('#datefrom').val();
                var DateTo = $('#dateto').val();
                var fileId = $('#fileId').html();
                
                $.ajax({
                    type: 'GET',
                    url: "Home/NumberDownloads/?dateFrom=" + DateFrom + "&dateTo=" + DateTo + "&fileId= " + fileId,
                    success: function (data) {
                        $('#downloads').empty();
                        $('#downloads').append(data);
                        console.log(data);
                    },
                    error: function (error) {
                        console.log(error);
                    }
                });
            });
        });
    </script>

有了这个,我实现了我想要的,但仅限于Model.Files.

我想对Model.Filesforeach 循环中的所有元素重复这个 Ajax 调用,或者简单地说调用这个 Ajax foreach 迭代。


推荐阅读