ksg
ksg

Reputation: 4067

Jquery Datatable -Is it possible to bind href property of link with razor syntax dynamically

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>'
            }

        }]

    });

Upvotes: 0

Views: 851

Answers (1)

markpsmith
markpsmith

Reputation: 4918

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.

Upvotes: 1

Related Questions