jquery - 从 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。
解决方案
<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.Files
foreach 循环中的所有元素重复这个 Ajax 调用,或者简单地说调用这个 Ajax foreach 迭代。
推荐阅读
- javascript - 基于 JavaScript 触发器停用元素 (css)
- python - 从大而丑陋的输入文本文件到非常小的数据框
- c# - 检查类型与 is 用于多个泛型类型
- android - sqlite 在尝试导入房间数据库时报告数据库损坏 android kotlin
- docker - 如何在docker镜像中查看libstdc++6的安装位置
- postgresql - 检查 Postgres DB 中是否存在数据
- c++ - VSCode C/C++ 扩展多项目包含路径错误
- c - 如何找到未定义的函数并将它们添加/链接为外部函数?
- c - 计算C中给定数字上存在多少特定数字
- codenameone - CodenameOne:PropertyIndex.toJSON() 没有为对象列表生成正确的 JSON