无法在mvc 4中将数据加载到jTable中

时间:2013-05-24 17:47:47

标签: jquery sql-server asp.net-mvc json jquery-jtable

好的,所以我第一次尝试使用jTable并且可以加载表格,但这对我来说非常好,因为它不会加载我的任何数据。当我调试程序时,我想要的表中的所有行都存储在我的列表中,所以当我运行我的应用程序说“在与服务器通信时发生错误”时,为什么会弹出一个对话框让我很困惑:

[HttpPost]
public JsonResult General_InfoList(int jtStartIndex = 0, int jtPageSize = 0, string jtSorting = null)
{
    try
    {
        // When debugging, all rows are successfully being stored in this list
        // but a dialog box pops up saying 'An error occured while communicating to the server.'
        Thread.Sleep(200);
        var genInfoCount = _repository.General_Info_Repository.GetGeneral_InfoCount();
        var genInfo = _repository.General_Info_Repository.GetGeneral_Info(jtStartIndex, jtPageSize, jtSorting);
        return Json(new { Result = "OK", Records = genInfo, TotalRecordCount = genInfoCount });

    }
    catch (Exception ex)
    {
        return Json(new { Result = "ERROR", Message = ex.Message });
     }
}

在我看来,我有:

<div id="General_InfoTableContainer">
</div>
<script type="text/javascript">

    $(document).ready(function () {

        $('#General_InfoTableContainer').jtable({
            title: 'General_Info List',
            paging: true,
            pageSize: 10,
            sorting: true,
            defaultSorting: 'Quote ID ASC',
            actions: {
                listAction: '@Url.Action("General_InfoList")',
                deleteAction: '@Url.Action("DeleteGeneral_Info")',
                updateAction: '@Url.Action("UpdateGeneral_Info")',
                createAction: '@Url.Action("CreateGeneral_Info")'
            },
            fields: {
                QuoteID: {
                    key: true,
                    create: false,
                    edit: false,
                    list: true,
                    title: 'ID',
                    width: '5%'
                },
                Open_Quote: {
                    title: 'Open Quote',
                    list: true,
                    width: '15%',
                    type: 'date',
                    displayFormat: 'mm-dd-yy'
                },
                Customer_Name: {
                    list: true,
                    title: 'Customer',
                    width: '25%'
                },
                OEM_Name: {
                    title: 'OEM',
                    list: true,
                    width: '25%'
                },
                Qty: {
                    title: 'Qty',
                    list: true,
                    width: '5%'
                },
                FD_Num: {
                    title: 'FD Num',
                    width: '10%',
                    list: true,
                    sorting: false
                },
                Rfq_Num: {
                    title: 'RFQ',
                    width: '10%',
                    list: true,
                    sorting: false
                },
                Rev_Num: {
                    title: 'Rev',
                    width: '5%',
                    list: true,
                    sorting: false
                },

                Score_Card: {
                    create: false,
                    edit: false,
                    list: false
                },
                Quantities: {
                    create: false,
                    edit: false,
                    list: false
                },
                QuoteAccesses: {
                    create: false,
                    edit: false,
                    list: false
                },
                Vendor_Input: {
                    create: false,
                    edit: false,
                    list: false
                }              
            }
        });

        //Load student list from server
        $('#General_InfoTableContainer').jtable('load');
    });

</script>

如果有人在我上面的代码中看到任何会阻止数据加载的内容,请告诉我们。 --Thanks

1 个答案:

答案 0 :(得分:3)

您在JsonRequestBehaviour.AllowGet声明中遗漏了return Json(...)。它似乎是可选的,但我发现实际上它是必需的。

return Json(new { Result = "OK", Records = genInfo }, JsonRequestBehaviour.AllowGet);

Bonus提示:当你从ajax调用中获得神秘的500时,请在Visual Studio中执行以下操作:

  1. 调试菜单 - &gt;例外...
  2. 选择“公共语言运行时例外”。
  3. 这将强制您的服务器代码在发生异常时停止。当然,它也可以显示误报,因此您可能希望在启用此功能时有所选择。