Jquery Datatable - 是否可以使用razor语法动态绑定链接的href属性 [英] Jquery Datatable -Is it possible to bind href property of link with razor syntax dynamically
问题描述
我正在使用 Jquery Datatables 插件.我正在显示员工详细信息列表,并允许使用上述插件进行编辑.我的问题是
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
是否可以根据 MVC 的 razor 语法动态设置编辑链接的 href 属性?
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 标记是
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>'
}
}]
});
推荐答案
您需要呈现这样的 HTML 链接(对任何引用错误表示歉意!):
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>';
full
参数是当前行的完整数据源,因此假设 MenuRoleId
是您要使用的 ID,您可以使用 full[5]
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]
您可以使用 Razor 语法来创建 url,但这意味着数据表 javascript 代码需要在 View 中,这并不理想.
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.
这篇关于Jquery Datatable - 是否可以使用razor语法动态绑定链接的href属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!