【问题标题】:How can I Change Id in Grid View To Value?如何将网格视图中的 ID 更改为值?
【发布时间】: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


【解决方案1】:

1.html元素的id应该是唯一的,所以你可以在Products列的每一行添加class="ProductId",然后像下面这样改变jquery:

<td class="ProductId"  name="ProductId">@item.ProductId</td>

<script>
    function GetProductName(id,index) {
        $.get('/OrderLine/GetProductNameById?ProductId=' + id, function (data) {
        }).always(function (result) {
            $(index).html(result);
        });
    }
    $('.ProductId').each(function(i, obj) {
      var MyVal = $(this).html();
        console.log(MyVal);
        GetProductName(MyVal,this);
   });
</script>

2.更好的方法,好像orderLine和Product之间的relationship是一一对应的,可以参考下面的model-design,使用Include()加载相关的产品数据得到ProductName 直接在视图中:

订单行

public class OrderLine
{
    public int Id { get; set; }
    public int Price { get; set; }
    public int Count { get; set; }
    public int ProductId { get; set; }
    public Product Product { get; set; }
}

控制器

public IActionResult OrderList()
    {
        var model = _context.OrderLine.Include(o=>o.Product).ToList();
        return View(model);
    }

查看

<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 class="ProductId" name="ProductId">@item.Product.Name</td>
            <td><a asp-controller="OderLine" asp-action="Index" asp-route-id="@item.Id">Detail</a></td>

        </tr>
    }
</table>

【讨论】:

猜你喜欢
相关资源
最近更新 更多
热门标签