MVC ajax返回请求的未知参数错误

时间:2016-05-31 16:07:39

标签: jquery ajax model-view-controller datatables

$(document).ready(function () {
    $("#getViewReportButton").click(function () {
        //alert("drop down = " + $('#ReportSelected').find(":selected").text());
        viewReports($('#ReportSelected').find(":selected").val());
    });
});

function viewReports(reportSelected) {
    var controller = "PortalReports";
    alert("View Reports = " + reportSelected);
    var tableSelector = "#datatable";

    var table = $(tableSelector).DataTable({
        "ajax": {
            "url": $("#basePath").val() + controller + "/GetReport"

            /*,    "dataScr": "data",
            "columns": [
                { "data": "MiPolicyTransactionId" },
                { "data": "PolicyReference" },
                { "data": "AgencyCode" }]*/
        }
    });
}

public JsonResult GetReport()
        {
            _context = new ReportDB();


            var result = _context.MiPolicyTransactions.Take(10).ToList();

            var theResults =
                result.Select(
                    x =>
                        new
                        {
                            MiPolicyTransactionId = x.MiPolicyTransactionId,
                            AgencyCode = x.AgencyCode,
                            CompanyId = x.CompanyId
                        }).ToList();

            //var results = new { value = 3, text = "Hello" };
            return Json(new { data = theResults}, JsonRequestBehavior.AllowGet);
        }


<h2>@ViewBag.Title</h2>
<p>Please choose a Report:</p>
<form  id="reportForm" method="post">
    <div class="fieldRow ui-widget-content ui-corner-all">
        <div class="field">
            @Html.DropDownListFor(x => x.ReportSelected, Model.ReportOptions, "Please Select")
            <input id="getViewReportButton" type="submit" value="View Report" />
            <span id="searchLoader" class="ajaxLoader"></span>
        </div>
    </div>
</form>
<br />
<table  id="datatable" class="display DataTable">
    <thead>
        <tr>
            <th>MiPolicyTransactionId</th>
            <th>PolicyReference</th>
            <th>AgencyCode</th>
        </tr>
    </thead>
    <tbody></tbody>

</table>

我有上面的代码,它不会呈现并给出请求的未知参数&#39; 0&#39;对于第0行。有关此错误的详细信息,请参阅http://datatables.net/tn/4

我徒劳地尝试改变一堆参数,但却无法让事情发挥作用。

{data: [{MiPolicyTransactionId: 18, AgencyCode: "CJPTEST", CompanyId: "BQ"},…]}
data
:
[{MiPolicyTransactionId: 18, AgencyCode: "CJPTEST", CompanyId: "BQ"},…]
0
:
{MiPolicyTransactionId: 18, AgencyCode: "CJPTEST", CompanyId: "BQ"}
1
:
{MiPolicyTransactionId: 19, AgencyCode: "CJPTEST", CompanyId: "BQ"}
2
:
{MiPolicyTransactionId: 20, AgencyCode: "CJPTEST", CompanyId: "BQ"}
3
:
{MiPolicyTransactionId: 21, AgencyCode: "CJPTEST", CompanyId: "BQ"}
4
:
{MiPolicyTransactionId: 22, AgencyCode: "CJPTEST", CompanyId: "BQ"}
5
:
{MiPolicyTransactionId: 23, AgencyCode: "CJPTEST", CompanyId: "BQ"}
6
:
{MiPolicyTransactionId: 24, AgencyCode: "CJPTEST", CompanyId: "BQ"}
7
:
{MiPolicyTransactionId: 25, AgencyCode: "CJPTEST", CompanyId: "BQ"}
8
:
{MiPolicyTransactionId: 26, AgencyCode: "CJPTEST", CompanyId: "BQ"}
9
:
{MiPolicyTransactionId: 27, AgencyCode: "CJPTEST", CompanyId: "BQ"}

XHR预览看起来很好,但是当发出错误的警告框关闭时,它就消失了,我只是想不出别的什么!

第一个错误 First error

第二次错误 Second error

转到此

Goes to this

1 个答案:

答案 0 :(得分:0)

来自@davidkonrad的优秀答案 Jquery DataTables.net: Failing to put negative int in table

您正在混合使用ajax选项和其他选项。而不是

ajax: {
    url: '@Url.Content("~/api/cas/nodes/all/lastcondition/")',
    "columns": [
        { "type" : "num", "data": "Id"},
        { "data": "Name" },
        { "data": "Alias" },
        { "data": "DateQuery" },
        { "type" : "num", "data": "Condition" },
        { "type" : "num", "data": "TimeSecLastCondition" }
    ]
})

你应该做

ajax: {
    url: '@Url.Content("~/api/cas/nodes/all/lastcondition/")'
},
columns: [
     { "type" : "num", "data": "Id"},
     { "data": "Name" },
     { "data": "Alias" },
     { "data": "DateQuery" },
     { "type" : "num", "data": "Condition" },
     { "type" : "num", "data": "TimeSecLastCondition" }
  ]
}