[英]Make jQuery datatable row an asp-action link when clicked
我正在尝试创建一个 jQuery 数据表,当用户单击表行时,资产 ID 将传递给 ViewAsset controller。 谢谢你的帮助
HTML 标记:
<table id="tblAsset">
<thead >
<tr>
<th>
Asset Name
</th>
<th>
Asset Type
</th>
<th>
Data Type
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
// Onclick function for each row
<tr onclick="ViewAsset(@item.Id)">
<td>
@Html.DisplayFor(modelItem => item.AssetName)
</td>
<td>
@Html.DisplayFor(modelItem => item.AssetType)
</td>
<td>
@Html.DisplayFor(modelItem => item.TypeofData)
</td>
</tr>
}
</tbody>
</table>
jQuery 数据表
<script type="text/javascript">
$(document).ready(function () {
$('#tblAsset').DataTable({
"pageLength":10,
"order": [[1, "asc"]],
}
);
});
// jQuery Function to call the controller
function ViewAsset(id) {
window.location.href = "Assets/ViewAsset/" + id;
}
</script>
Controller 动作链接
<a asp-action="ViewAsset" asp-route-id="@item.Id">
要处理每一行的点击事件,请为标签添加一个 onlick 事件。 创建一个 js function 并将您尝试达到的操作的 url 指定为参数。
//Razor view
<tr onclick='CallController("@Url.Action("ViewAsset", "YourController", new { id = "123" }))"'></tr>
//JS CODE
function CallController(url){
$.ajax({
url: url,
complete: function () {
}
});
}
如果您想在单击该行时重定向到其他视图,您可以直接使用window.location.href
来访问 controller 动作。
<tr onclick="ViewAsset(@item.Id)">
脚本:
<script type="text/javascript">
$(document).ready(function () {
$('#tblAsset').DataTable({
"pageLength": 10,
"order": [[1, "asc"]],
}
);
});
function ViewAsset(id) {
window.location.href = "ControllerName/ActionName/" + id;
}
</script>
我找到了解决问题的更好方法。 为以后有类似问题的人发帖
<tr style="cursor: pointer;" onclick="location.href ='@(Url.Action("ViewAsset", "Assets", new { id = item.Id}))'">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.