首页 > 解决方案 > 如何在控制器中获取数据表的参数?

问题描述

我在视图中使用databale

   $("#table-DoctorClosure").DataTable({
            "bServerSide": true,
            "sAjaxSource": "@Url.Action("PaginationList", "DoctorClosure")",
            "fnServerData": function (sSource, aoData, fnCallback) {
                aoData.push({ "name": "DoctorId", "value": @ViewBag.doctorId} );
                $.ajax({
                    type: "Get",
                    data: aoData,
                    url: sSource,
                    success: fnCallback
                })
            },
            "aoColumns": [
                { "mData": "Date" },
                { "mData": "Description" },
                {
                    "mData": "Id",
                    "className": "text-center ",
                    "render": function(Id, type, full, meta) {
                        return '<a class="btn-edit  btn btn-warning btn-sm m-l-5" href="/Doctor/DoctorClosure/Edit?id=' + Id +'" data-toggle="tooltip" data-placement="top" title="@OperationResource.Edit"><i class="icon-pencil" ></i> </a>' +
                            '<a class="btn-delete btn btn-sm bg-danger"  data-id="'+Id+'" data-toggle="tooltip" data-placement="top" title="@OperationResource.Delete"><i class="icon-trash-o"></i> </a>'
                    }
                },
            ],
            "oLanguage": {
                "sUrl": "/Content/styles/datatables/dataTables.persian.txt"
            }
        });

我想得到 DoctorId 的aoData

  public async Task<ActionResult> PaginationList(DataTableParameter param)
  {
   ???
  }

DataTableParameter

 public  class DataTableParameter
{
    /// <summary>
    /// Request sequence number sent by DataTable,
    /// same value must be returned in response
    /// </summary>
    public string sEcho { get; set; }
    /// <summary>
    /// Text used for filtering
    /// </summary>
    public string sSearch { get; set; }
    /// <summary>
    /// Number of records that should be shown in table
    /// </summary>
    public int iDisplayLength { get; set; }
    /// <summary>
    /// First record that should be shown(used for paging)
    /// </summary>
    public int iDisplayStart { get; set; }
    /// <summary>
    /// Number of columns in table
    /// </summary>
    public int iColumns { get; set; }
    /// <summary>
    /// Number of columns that are used in sorting
    /// /// </summary>
    public int iSortingCols { get; set; }
    /// <summary>
    /// Comma separated list of column names
    /// </summary>
    public string sColumns { get; set; }
}

标签: asp.net-mvcmodel-view-controllerdatatabledatatables

解决方案


如果我正确理解了您的问题,那么当您将数据发送到控制器(使用 ajax 或无)时,您应该在输入控制器中有一个变量。您发送数据并且您DoctorId不在DataTableParameter课堂上。所以如果你想得到这个参数,你有两个解决方案:

首先是在您的课程中添加 DoctorId(或使用此参数制作视图模型):

 public  class DataTableParameter
{
    /// <summary>
    /// Request sequence number sent by DataTable,
    /// same value must be returned in response
    /// </summary>
    public string sEcho { get; set; }
    /// <summary>
    /// Text used for filtering
    /// </summary>
    public string sSearch { get; set; }
    /// <summary>
    /// Number of records that should be shown in table
    /// </summary>
    public int iDisplayLength { get; set; }
    /// <summary>
    /// First record that should be shown(used for paging)
    /// </summary>
    public int iDisplayStart { get; set; }
    /// <summary>
    /// Number of columns in table
    /// </summary>
    public int iColumns { get; set; }
    /// <summary>
    /// Number of columns that are used in sorting
    /// /// </summary>
    public int iSortingCols { get; set; }
    /// <summary>
    /// Comma separated list of column names
    /// </summary>
    public string sColumns { get; set; }

    /// <summary>
    /// if you use this you can send and use it in controller
    /// </summary>
    public int DoctorId { get; set; }
}

第二种方法是在控制器输入中添加另一个参数:

public async Task<ActionResult> PaginationList(DataTableParameter param, int DoctorId)
  {
   ???
  }

推荐阅读