Jquery Datatable -Is it possible to bind href prop

2019-03-03 08:02发布

I am using Jquery Datatables plugin. I am showing list of employee details with an provision to edit using the above plugin. My questioon is

Is it possible to set href property of the edit link dynamically along with razor syntax of MVC ?

If not what is the alternate solution to navigate to edit page ?

HTML markup is

        table = $("#dataTableMenuRole").dataTable({
        "ajax": {
            "url": "@Url.Action("LoadGridView", "MenuSettings")",                
            "method": "POST",
            "dataType": "json",
            "data": function (d) {
                d.roleId = $("#ddlRoles").val()
            }
        },
        columns: [
            { "data": "MenuName" },
            { "data": "CanAdd" },
            { "data": "CanEdit" },
            { "data": "CanDelete" },
            { "data": "CanView" },
            { "data": "MenuRoleId" }

        ],            
        "bProcessing": true,
        "aoColumnDefs": [{
            "targets": 5,
            "bSortable": false,
            "render": function (data, type, full, meta) {
                return '<a class="btn btn-info" 
                href="@Url.Action("Edit", new {id= data })"' >'+
               //Here data cannot be assigned as shown
                        '<i class="fa fa-edit"></i>'+
                    '</a>'
            }

        }]

    });

1条回答
够拽才男人
2楼-- · 2019-03-03 08:43

You need to render an HTML link like this (apologies for any quote errors!):

return '<a class="btn btn-info" href="/Edit/' + full[5] + '"><i class="fa fa-edit"></i></a>';

The full parameter is the full datasource for the current row, so assuming MenuRoleId is the Id you want to use, you access it using full[5]

You could use Razor syntax to create the url, but it means that the datatables javascript code would need to be in View, which is not ideal.

查看更多
登录 后发表回答