【问题标题】:How to pass row id by href link in mvc from view to controller如何通过mvc中的href链接将行ID从视图传递到控制器
【发布时间】:2019-03-26 06:13:34
【问题描述】:

我想通过 mvc 中数据表中的 href 链接将行 ID 从视图传递到控制器。

这是我的数据表设计代码

<table id="dataGrid" class="table table-striped table-bordered dt-responsive nowrap" width="100%" cellspacing="0">
    <thead>
        <tr>
            <th>N</th>
            <th>Date</th>
            <th>Action</th>
        </tr>
    </thead>
</table>

这是数据表操作代码

$(document).ready(function () {
    $("#dataGrid").DataTable({
        "processing": true, // for show progress bar
        "serverSide": true, // for process server side
        "pageLength": 5,
        "lengthMenu": [5, 10, 50, 100, 1000, 10000],

        "columnDefs":
        [{
            "targets": [0],
            "visible": false,
            "searchable": false,
            "orderable": false
        }
        ],
        "columns": [
            { "data": "id", "name": "Id", "autoWidth": true, "orderable": false },
            { "data": "date", "name": "Date", "autoWidth": true, "orderable": false, "format": "dd/MM/yyyy" },
                "render": function (data, type, row) {
                    return " <a href='/ServiceJob/GetPrintById?'" + row.id + "'' class='btn btn-success btn-lg glyphicon glyphicon-print'> Print </a> ";
                }
            },
        ]
    });
});

这里是控制器

[HttpPost]
public IActionResult GetPrintById(int id)
{
    ServiceJobModel model = new ServiceJobModel();

    // Service Job Detail
    var serviceJob = _Db.ServiceJob.Where(x => x.Id == id).FirstOrDefault();

    model.Id = serviceJob.Id;
    model.Date = serviceJob.Date;

    return View("Print");
}

现在在数据表操作代码中,我尝试通过以下代码作为回报

<a href='/ServiceJob/GetPrintById?'" + row.id + "'' class='btn btn-success btn-lg glyphicon glyphicon-print'> Print </a>

但上面的代码不起作用。

如果我使用 actionlink 代替 href,那么当时它会显示错误,即 row 在当前上下文中不存在强>。

这里是actionlink代码,

@Html.ActionLink("Print", "GetPrintById", "ServiceJob", new { id = row.id }, null)

所以,对我来说,这两个代码都没有执行。

帮我解决这个问题。

谢谢。

【问题讨论】:

    标签: asp.net-mvc razor datatables href actionlink


    【解决方案1】:

    row 是一个客户端变量,您不能在运行服务器端的@Html.ActionLink() 助手中使用它。您可以使用@Url.Action() helper 添加行ID,并将包含完整URL 的变量设置为render 设置内的锚标记href 属性:

    "render": function (data, type, row) {
        var url = '@Url.Action("GetPrintById", "ServiceJob")/' + row.id; 
    
        return "<a href='" + url + "' class='btn btn-success btn-lg glyphicon glyphicon-print'>Print</a> ";
    }
    

    更新:

    注意锚标签always使用GET请求调用href属性中提到的控制器动作的路径,因此控制器动作上的[HttpPost]属性必须省略/删除:

    public IActionResult GetPrintById(int id)
    {
        ServiceJobModel model = new ServiceJobModel();
    
        // Service Job Detail
        var serviceJob = _Db.ServiceJob.Where(x => x.Id == id).FirstOrDefault();
    
        model.Id = serviceJob.Id;
        model.Date = serviceJob.Date;
    
        return View("Print");
    }
    

    参考:

    Datatables with asp.net mvc rendering Url.actions/html.actionlinks with route values

    【讨论】:

    • 通过执行此错误不会出现,但在 GetPrintById 方法中的控制器中未触发操作。
    • 锚链接始终使用 GET 请求,只需删除 [HttpPost] 属性,控制器动作就会触发。
    • 好兄弟。非常感谢xxx提供的重要信息。
    猜你喜欢
    • 1970-01-01
    • 2015-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-09
    • 2015-12-23
    • 2017-05-27
    • 2016-12-28
    相关资源
    最近更新 更多