[英]How can I select a row from a jquery-datatable, get more data from the controller and show them under the table? (mvc4)
I've got a MVC4-project and I'm using the jquery/Datatables-plugin. 我有一个MVC4项目,正在使用jquery / Datatables-plugin。
I've got a viewmodel with the next properties: 我有一个具有以下属性的视图模型:
public IEnumerable<Invoice> Invoices { get; set; }
public IProcessResult Result { get; set; }
public Invoice Invoice { get; set; }
The datatable shows all the invoices (from model.Invoices) and under this table I show the Invoice-details of the selected invoice (and some other information like images). 数据表显示了所有发票(来自model.Invoices),在此表下,我显示了所选发票的发票明细(以及一些其他信息,例如图像)。
Now I want to show the Invoice-details from the selected Invoice in the datatable (after clicking the invoice). 现在,我想在数据表中显示所选发票的发票明细(单击发票后)。 Before this, I have to initialise some data (in the controller). 在此之前,我必须初始化一些数据(在控制器中)。
I've got this javascriptcode: 我有这个javascript代码:
$(document).ready(function () {
$('#invoicesoverview tr').click(function () {
oTable.$("tr.row_selected").removeClass("row_selected");
$(this).toggleClass('row_selected');
});
var oTable = $('#invoicesoverview').dataTable({
"bPaginate": true,
"iDisplayLength": 10,
"bSort": false,
"bFilter": false,
"bResetDisplay": false,
"bServerSide": false,
"bProcessing": true
});
oTable.$("tr:first").trigger("click");
}
I'm a 'MVC4-starter', so I don't know what to do next. 我是“ MVC4-入门者”,所以我不知道下一步该怎么做。 I think I have to call a ajax-call from the click-event, but I don't know how to do this. 我想我必须从点击事件中调用ajax调用,但是我不知道该怎么做。
How I would do it is first get the id of the row that was clicked on. 我要怎么做,首先要获得被单击的行的ID。 here is a link that discusses that Get id of selected row in a table -HTML . 这是一个讨论如何获取表-HTML中选定行的ID的链接 。 then in your click function add an ajax call 然后在您的点击功能中添加一个ajax调用
$.ajax({
url: "@(Url.Action("Details", "Controller", new { id = "----" }))/".replace("----", id),
type: "POST",
cache: false,
async: true,
success: function (result) {
$(".DetailsDiv").html(result);
}
});
Create a partial view to display the details of the row that was clicked and in your controller 创建局部视图以显示单击的行以及控制器中的详细信息
[HttpPost]
public PartialViewResult Details(string id)
{
Model model = (populate your data);
return PartialView("_PartialView", model);
}
Hopefully this helps. 希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.