简体   繁体   English

如何从jquery-datatable中选择一行,从控制器中获取更多数据并在表下显示它们? (mvc4)

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

相关问题 如何从视图获取数据到控制器mvc4? - How to get data from view to controller mvc4? 如何在MVC4中将视图中的列表返回到控制器 - How can I return a list from view to controller in MVC4 从控制器动作获取数据到jquery数据表 - Get data from controller action to jquery datatable 如何使用MVC4在Controller中获得主机 - How can I get host in a Controller using MVC4 如何向使用MS Access表中的数据填充的数据表添加新行 - How can I add a new row to a datatable that I populated with data from a MS Access table 我如何使用 datapost 将参数从 jqgrid 传递到 controller(使用 MVC4 和 asp.net) - how can i pass parameter from jqgrid to controller with datapost (using MVC4 and asp.net ) 我如何从MVC中的表中获取数据 - How I can get data from table in MVC 使用 getJSON MVC4 C#/Jquery 从控制器动态加载选择列表 - Load select list dynamically from controller using getJSON MVC4 C#/Jquery 如何在单击 C# MVC 中的行中的按钮时将表行中的数据插入数据库 - How can i insert data from a table row into Database on clicking a button in the row in C# MVC 如何将数据从Select2选项/值(标签)获取到ASP.NET MVC中的控制器? - How do I get the data from a Select2 options/value (tags) to the controller in asp.net mvc?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM