【发布时间】:2019-03-26 06:13:34
【问题描述】:
我想通过 mvc 中数据表中的 href 链接将行 ID 从视图传递到控制器。
这是我的数据表设计代码
<table id="dataGrid" class="table table-striped table-bordered dt-responsive nowrap" width="100%" cellspacing="0">
<thead>
<tr>
<th>N</th>
<th>Date</th>
<th>Action</th>
</tr>
</thead>
</table>
这是数据表操作代码
$(document).ready(function () {
$("#dataGrid").DataTable({
"processing": true, // for show progress bar
"serverSide": true, // for process server side
"pageLength": 5,
"lengthMenu": [5, 10, 50, 100, 1000, 10000],
"columnDefs":
[{
"targets": [0],
"visible": false,
"searchable": false,
"orderable": false
}
],
"columns": [
{ "data": "id", "name": "Id", "autoWidth": true, "orderable": false },
{ "data": "date", "name": "Date", "autoWidth": true, "orderable": false, "format": "dd/MM/yyyy" },
"render": function (data, type, row) {
return " <a href='/ServiceJob/GetPrintById?'" + row.id + "'' class='btn btn-success btn-lg glyphicon glyphicon-print'> Print </a> ";
}
},
]
});
});
这里是控制器
[HttpPost]
public IActionResult GetPrintById(int id)
{
ServiceJobModel model = new ServiceJobModel();
// Service Job Detail
var serviceJob = _Db.ServiceJob.Where(x => x.Id == id).FirstOrDefault();
model.Id = serviceJob.Id;
model.Date = serviceJob.Date;
return View("Print");
}
现在在数据表操作代码中,我尝试通过以下代码作为回报
<a href='/ServiceJob/GetPrintById?'" + row.id + "'' class='btn btn-success btn-lg glyphicon glyphicon-print'> Print </a>
但上面的代码不起作用。
如果我使用 actionlink 代替 href,那么当时它会显示错误,即 row 在当前上下文中不存在强>。
这里是actionlink代码,
@Html.ActionLink("Print", "GetPrintById", "ServiceJob", new { id = row.id }, null)
所以,对我来说,这两个代码都没有执行。
帮我解决这个问题。
谢谢。
【问题讨论】:
标签: asp.net-mvc razor datatables href actionlink