Asp.net mvc 如何通过mvc中的href链接将行id从视图传递到控制器
我想在mvc中通过数据表中的href链接将行Id从视图传递到控制器 这是我的数据表设计代码Asp.net mvc 如何通过mvc中的href链接将行id从视图传递到控制器,asp.net-mvc,razor,datatables,href,actionlink,Asp.net Mvc,Razor,Datatables,Href,Actionlink,我想在mvc中通过数据表中的href链接将行Id从视图传递到控制器 这是我的数据表设计代码 <table id="dataGrid" class="table table-striped table-bordered dt-responsive nowrap" width="100%" cellspacing="0"> <thead> <tr> <th>N</th>
<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>
现在,在datatable操作代码中,我尝试使用以下代码作为回报
<a href='/ServiceJob/GetPrintById?'" + row.id + "'' class='btn btn-success btn-lg glyphicon glyphicon-print'> Print </a>
因此,对我来说,这两个代码都没有执行
帮我解决这个问题
谢谢。行是客户端变量,您不能在运行服务器端的
@Html.ActionLink()
帮助程序中使用它。您可以使用@Url.Action()
helper添加行ID,并将包含完整Url的变量设置到呈现设置中锚定标记的href
属性中:
"render": function (data, type, row) {
var url = '@Url.Action("GetPrintById", "ServiceJob")/' + row.id;
return "<a href='" + url + "' class='btn btn-success btn-lg glyphicon glyphicon-print'>Print</a> ";
}
参考:
执行此操作时,不会出现错误,但不会在GetPrintById方法的控制器中触发操作。锚链接始终使用GET请求,只需删除[HttpPost]
属性,控制器操作就会触发。awsome brother。非常感谢XXX提供的大量信息。
<a href='/ServiceJob/GetPrintById?'" + row.id + "'' class='btn btn-success btn-lg glyphicon glyphicon-print'> Print </a>
@Html.ActionLink("Print", "GetPrintById", "ServiceJob", new { id = row.id }, null)
"render": function (data, type, row) {
var url = '@Url.Action("GetPrintById", "ServiceJob")/' + row.id;
return "<a href='" + url + "' class='btn btn-success btn-lg glyphicon glyphicon-print'>Print</a> ";
}
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");
}