[英]Make jQuery datatable row an asp-action link when clicked
I am trying to create a jQuery datable that when user clicked on a table row the asset Id is passed to the ViewAsset controller.我正在尝试创建一个 jQuery 数据表,当用户单击表行时,资产 ID 将传递给 ViewAsset controller。 Thanks for your help
谢谢你的帮助
HTML markup: 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 datatable 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 action link Controller 动作链接
<a asp-action="ViewAsset" asp-route-id="@item.Id">
To handle a click event for every row add an onlick event for the tag.要处理每一行的点击事件,请为标签添加一个 onlick 事件。 Create a js function and specify as the parameter the url of the action you're trying to reach.
创建一个 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 () {
}
});
}
If you want to redirect to other view when click the row, you can directly use window.location.href
to acces the controller action.如果您想在单击该行时重定向到其他视图,您可以直接使用
window.location.href
来访问 controller 动作。
<tr onclick="ViewAsset(@item.Id)">
Scripts:脚本:
<script type="text/javascript">
$(document).ready(function () {
$('#tblAsset').DataTable({
"pageLength": 10,
"order": [[1, "asc"]],
}
);
});
function ViewAsset(id) {
window.location.href = "ControllerName/ActionName/" + id;
}
</script>
I found a better way to solve the issue.我找到了解决问题的更好方法。 Post for people with similar issue in the future
为以后有类似问题的人发帖
<tr style="cursor: pointer;" onclick="location.href ='@(Url.Action("ViewAsset", "Assets", new { id = item.Id}))'">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.