Can't get data to load into jTable in mvc 4

江枫思渺然 提交于 2019-12-11 02:57:01

问题


Okay, so I'm trying out jTable for the first time and I can get the table to load, but that does me very little good because it won't load with any of my data. When I debug the program, all of the rows from the table I want are getting stored in my List so I'm confused as to why a dialog box pops up when I run my application saying 'An error occured while communicating to the server':

[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 });
     }
}

And in my view I have:

<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>

If anyone sees anything in my code above that would prevent the data from loading please let me know. --Thanks


回答1:


You are missing JsonRequestBehaviour.AllowGet from your return Json(...) statement. It seems that it's optional, but I find in reality it's required.

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

Bonus Hint: when you get mysterious 500's from your ajax calls, do the following inside Visual Studio:

  1. Debug menu->Exceptions...
  2. Select 'Common Language Runtime Exceptions'.

This will force your server code to stop when/if an exception occurs. Of course, it can also show you false positives, so you may want to be selective on when you turn this on.



来源:https://stackoverflow.com/questions/16740808/cant-get-data-to-load-into-jtable-in-mvc-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!