[英]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.