【问题标题】:Requested unknown parameter error in JQuery Datatable MVCJQuery Datatable MVC 中请求的未知参数错误
【发布时间】:2017-12-25 09:26:45
【问题描述】:

我正在处理数据表并遇到此错误

DataTables 警告:表 id=DataGridTable - 请求第 0 行第 0 列的未知参数“TranID”。

其他人说我在 HTML 中定义的表格列与从服务器接收到的列不匹配。但就我而言,两者都是一样的。

请帮忙!谢谢!

控制器:

public IActionResult CompanyProfileView(jQueryDataTableParamModel param)
    {
        CompanyProfileModel objCompanyProfileModel = new CompanyProfileModel();

        string strResXML = "";
        string strErrMsg = "";

        try
        {
            objCompanyProfileModel.CompanyProfileView(ref strResXML, ref strErrMsg);
            if (strErrMsg != "") throw (new ApplicationException(strErrMsg));

            TextReader sr = new StringReader(strResXML);
            XElement root = XElement.Load(sr);

            IEnumerable<XElement> allRecords =
               from el in root.Elements("RESULT_ROW")
               select el;

            IEnumerable<XElement> filteredRecords = allRecords;

            if (!string.IsNullOrEmpty(param.sSearch))
            {
                filteredRecords = from el in allRecords
                                .Where(el =>
                                       el.Element("TranID").Value.ToLower().Contains(param.sSearch.ToLower()) ||
                                       el.Element("CompanyID").Value.ToLower().Contains(param.sSearch.ToLower()) ||
                                       el.Element("CompanyNm").Value.ToLower().Contains(param.sSearch.ToLower()) ||
                                       el.Element("CompanySubNm").Value.ToLower().Contains(param.sSearch.ToLower()) ||
                                       el.Element("CompType").Value.ToLower().Contains(param.sSearch.ToLower()))
                                  select el;
            }

            var displayedRecords = filteredRecords
                                .Skip(param.iDisplayStart)
                                .Take(param.iDisplayLength);

            var sortColumnIndex = Convert.ToInt32(Request.Form["iSortCol_0"]);
            Func<XElement, string> orderingFunction = (c => sortColumnIndex == 0 ? (c.Element("TranID") != null) ? c.Element("TranID").Value.ToString() : "" :
                                                            sortColumnIndex == 1 ? (c.Element("CompanyID") != null) ? c.Element("CompanyID").Value.ToString() : "" :
                                                            sortColumnIndex == 2 ? (c.Element("CompanyNm") != null) ? c.Element("Purpose").Value.ToString() : "" :
                                                            sortColumnIndex == 3 ? (c.Element("CompanySubNm") != null) ? c.Element("CompanySubNm").Value.ToString() : "" :
                                                            (c.Element("CompType") != null) ? c.Element("CompType").Value.ToString() : "");
            var sortDirection = Request.Form["sSortDir_0"]; // asc or desc
            if (sortDirection == "asc")
                displayedRecords = displayedRecords.OrderBy(orderingFunction);
            else
                displayedRecords = displayedRecords.OrderByDescending(orderingFunction);

            var result = from c in displayedRecords
                 select new
                 {
                    TranID = (c.Element("TranID") != null) ? c.Element("TranID").Value.ToString().Trim() : "",
                    CompanyID = (c.Element("CompanyID") != null) ? c.Element("CompanyID").Value.ToString().Trim() : "",
                    CompanyNm = (c.Element("CompanyNm") != null) ? c.Element("CompanyNm").Value.ToString().Trim() : "",
                    CompanySubNm = (c.Element("CompanySubNm") != null) ? c.Element("CompanySubNm").Value.ToString().Trim() : "",
                    CompType = (c.Element("CompType") != null) ? c.Element("CompType").Value.ToString() : "", 
                    Action = "Action",
                 };



            return Json(new
            {
                sEcho = param.sEcho,
                iTotalRecords = allRecords.Count(),
                iTotalDisplayRecords = filteredRecords.Count(),
                aaData = result
            });
        }
        catch (Exception e)
        {
            return Json(new
            {
                sMsg = e.Message,
                sEcho = param.sEcho,
                iTotalRecords = 0,
                iTotalDisplayRecords = 0,
                aaData = ""
            });
        }
    }

阿贾克斯:

$('#DataGridTable').dataTable({ 
        "bServerSide": true,
        "bProcessing": true,
        "bFilter": false,
        "bPaginate": true,
        "sPaginationType": "full_numbers",
        "sAjaxSource": "/DataGrid/CompanyProfileView", 
        "fnServerData": function (sSource, aoData, fnCallback) {
            $.ajax({
                "dataType": 'json',
                "type": "POST",
                "url": sSource,
                "data": aoData, 
                "success": fnCallback  
            });
        },
        "aoColumnDefs" : [ 
             { sTitle  : "TRANSACTION ID", sType : "string", mData : "TranID",       aTargets: [0], sClass  : 'centered-cell' },   
             { sTitle  : "COMPANY ID",     sType : "string", mData : "CompanyID",    aTargets: [1], sClass  : 'centered-cell', sDefaultContent: "" }, 
             { sTitle  : "COMPANY NAME",   sType : "string", mData : "CompanyNm",    aTargets: [2], sClass  : 'centered-cell', sDefaultContent: "" }, 
             { sTitle  : "SUB NAME",       sType : "string", mData : "CompanySubNm", aTargets: [3], sClass  : 'centered-cell', sDefaultContent: "" }, 
             { sTitle  : "COMPANY TYPE",   sType : "string", mData : "CompType",     aTargets: [4], sClass  : 'centered-cell', sDefaultContent: "" }, 
             { sTitle  : "ACTION",         sType : "string", mData : "Action",       aTargets: [5], sClass  : 'centered-cell', sDefaultContent: "" },
        ],  
        "fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
            $('td:eq(5)', nRow).html('<button class=\"btn btn-primary btn-xs\" ><img src=\"/dist/img/edit.png\" width=\"20px\" height=\"20px\"/></button>&nbsp;<button class=\"btn btn-danger btn-xs\" ><img src=\"/dist/img/delete.png\" width=\"20px\" height=\"20px\"/></button>');
         }
    });

HTML:

<table id="DataGridTable" class="display">  
                            <thead>  
                                <tr>  
                                    <th class="gridheader" style="text-align:center">TRANSACTION ID</th>  
                                    <th class="gridheader" style="text-align:center">COMPANY ID</th>  
                                    <th class="gridheader" style="text-align:center">COMPANY NAME</th>  
                                    <th class="gridheader" style="text-align:center">SUB NAME</th>  
                                    <th class="gridheader" style="text-align:center">COMPANY TYPE</th> 
                                    <th class="gridheader" style="text-align:center">ACTION</th>
                                </tr>  
                            </thead> 
                            <tbody></tbody> 
                        </table>

【问题讨论】:

    标签: jquery ajax asp.net-mvc


    【解决方案1】:

    我检查了实际响应,我得到了这个:

    {
    "sEcho": "1",
    "iTotalRecords": 2,
    "iTotalDisplayRecords": 2,
    "aaData": [
        {
            "tranID": "1000-12102017-000001",
            "companyID": "COMP1",
            "companyNm": "SAMPLE",
            "companySubNm": "",
            "compType": "SCHOOL",
            "action": "Action"
        },
        {
            "tranID": "1000-12102017-000002",
            "companyID": "COMP2",
            "companyNm": "SAMPLE COMPANY",
            "companySubNm": "",
            "compType": "COMPANY",
            "action": "Action"
        }
    ]
    

    }

    我刚刚更改了 mData

    mData : "TranID", to mData : "tranID",
    

    它的工作原理...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-29
      • 1970-01-01
      • 2019-09-16
      • 1970-01-01
      • 2014-04-03
      • 1970-01-01
      相关资源
      最近更新 更多