【发布时间】:2020-01-07 15:25:41
【问题描述】:
如何将网格视图中的 ID 更改为值? 下面的代码只针对第一条记录运行,我怎样才能为所有记录运行它?
我的操作代码:
public JsonResult GetProductNameById(int ProductId)
{
var productname = producteRepository.GetAll().Where(c => c.Id == ProductId).FirstOrDefault();
return Json(productname.Name);
}
网格:
我的查看代码:
@model List<ItcNetworkMarketing.Domain.Core.orderLine>
@if (Model != null && Model.Count > 0)
{
<table class="table table-bordered table-striped">
<tr>
<th>Count</th>
<th>Price</th>
<th>Products</th>
<th>Detail</th>
</tr>
@foreach (var item in Model)
{
<tr id="Mytr">
<td>@item.Count</td>
<td>@item.Price</td>
<td id="ProductId" name="ProductId" >@item.ProductId</td>
<td><a asp-controller="OderLine" asp-action="Index" asp-route-id="@item.Id">Detail</a></td>
</tr>
}
</table>
}
<script src="~/scripts/jquery-3.4.1.js"></script>
<script>
function GetProductName(id) {
$.get('/OrderLine/GetProductNameById?ProductId=' + id, function (data) {
}).always(function(result) {
$("#ProductId").html(result);
});
}
$("#ProductId").add(function () {
var MyVal = $("#ProductId").html();
console.log(MyVal);
GetProductName(MyVal);
});
</script>
【问题讨论】:
-
请注意,对于我们这些可以追溯到 ASP.net 网络表单的老学生来说,GridViews 是非常具体的东西,但事实并非如此。
标签: c# jquery asp.net asp.net-core