jquery - 链接到 Razor 页面的 JQuery 数据表行
问题描述
我正在使用带有 Razor 页面和 SQL 服务器的 JQuery 数据表 1.10.25 的当前版本。我无法弄清楚如何使数据表行可选择用于 CRUD。我需要将每一行路由到相同的 Razor 页面以进行更新,但还需要从行中携带数据。我如何在之后的语法
$(document).ready(function () {
$("#myTable").DataTable({ *my information*})
路由到 Razor 更新页面,并将行数据传递到页面?
该表填充了我的 SQL 服务器信息,但我似乎无法添加我需要的路由/传递。
先感谢您。
解决方案
这是一个将行数据传递给处理程序的演示:
模型:
public class Test
{
public int Id { get; set; }
public string Name { get; set; }
}
cshtml:
@Html.AntiForgeryToken()
<select id="ddlGroup">
<option>Choose Id</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<table id="taskDt">
<thead>
<tr>
<td>Id</td>
<td>Name</td>
<td>Action</td>
</tr>
</thead>
<tbody></tbody>
</table>
js:
var table;
function LoadData() {
table = $("#taskDt").DataTable({
"ajax": {
url: '?handler=GET',
type: 'GET',
processing: true,
serverSide: true,
},
"columns": [
{ "data": "id", "width": "50%" },
{ "data": "name", "width": "50%" },
{
render: function (data) {
return `
<button type="button" onclick=getData(this) class="btn btn-primary">
Edit Staff
</button>
`;
}
},
],
paging: true,
responsive: true,
searching: true,
ordering: true,
order: [[1, "asc"]]
});
}
function getData(t) {
var data = table.row($(t).parents('tr')).data();
console.log(data);
$.ajax({
type: "POST",
url: '?handler=ChangeData',
headers: { "RequestVerificationToken": $('input[name="__RequestVerificationToken"]').val() },
data: data,
success: function (data) {
},
error: function (result) {
alert("fail");
}
})
}
$(document).ready(function () {
LoadData();
})
然后你可以在处理程序中获取行数据public void OnPostChangeData(Test t)
推荐阅读
- php - 不和谐 oauth2 与 php
- google-apps-script - 在谷歌应用脚本中使用 Filter whenTextNotEqualToAny() 函数的布尔条件错误
- html - 尽管有百分比信息,但网站在不同屏幕上的格式不同
- reactjs - 不在反应选择中呈现选择值
- amazon-web-services - 在 ECS fargate 任务中解析服务主机名
- sql-server - SQL Server 2019 Enterprise - 具有主密钥的高可用性
- c++ - Distance to representative in Disjoint set union data structure
- search - 从 BST 中删除根节点
- sql-server - 在 SQL Server 2016 中使用 GROUP BY 和 FOR XML PATH
- android - 如何以有条件的方式懒惰地或通常地分配属性