1

I have a problem with my jqGrid. I have seen other posts here with similar problem, except that mine is particular in that the data is loading correctly in my development machine but when I publish the site on my production server, the jqGrid won't load the data, all I get is an empty grid. Every other ajax request for data in the server works fine, except for the jqGrid. My project is in MVC3 and I am hosting the site in win2008 IIS7. This is my current code:

View:

<script type="text/javascript">
$(document).ready(function () {
    $("#grid").jqGrid({
        url: '@Url.Action("LoadAction", "Controller", new { area = "Area" })',
        editurl: '@Url.Action("SaveAction", "Controller", new { area = "Area" })',
        datatype: 'json',
        mtype: 'POST',
        colNames: [
            '@Html.LabelFor(v => new foo.bar.MyClass().Property1)',
            '@Html.LabelFor(v => new foo.bar.MyClass().Property2)',
            '@Html.LabelFor(v => new foo.bar.MyClass().Property3)',
            '@Html.LabelFor(v => new foo.bar.MyClass().Property4)'
        ], 
        colModel: [
            { name: 'Property1', index: 'Property1', editable: true },
            { name: 'Property2', index: 'Property2', editable: true, edittype: "select", editoptions: { value: "Option1:Option1;Option2:Option2"} },
            { name: 'Property3', index: 'Property3', editable: true },
            { name: 'Property4', index: 'Property4', editable: true }
        ],
        pager: $('#gridPager'),
        rowNum: 20,
        rowList: [10, 20, 50, 100],
        sortname: 'Property1',
        sortorder: "asc",
        viewrecords: true,
        imgpath: '',
        shrinkToFit: true,
        width: 940,
        height: 300,
        gridview: true
    });
});
</script>
<div>
    <table id="grid" class="scroll" cellpadding="0" cellspacing="0"></table>
    <div id="gridPager" class="scroll" style="text-align:center;"></div>
</div>

Controller:

[Authorize]
public JsonResult LoadAction(string sidx, string sord, int page, int rows)
{
    List<foo.bar.MyClass> list = foo.bar.MyClassController.getAll();

    int pageIndex = Convert.ToInt32(page) - 1;
    int pageSize = rows;
    int totalRecords = list.Count();
    int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);

    var jsonData = new
    {
        total = totalPages,
        page,
        records = totalRecords,
        rows = (
            from myclass in list
            select new
            {
                id = myclass.Id,
                cell = new string[] { 
                    myclass.Property1, 
                    myclass.Property2, 
                    myclass.Property3, 
                    myclass.Property4
                }
            }).ToArray()
    };
    return Json(jsonData);
}

Anyone have any idea what might be the problem?

Thank you.

AJC
  • 1,853
  • 1
  • 17
  • 28

2 Answers2

2

1) I recommend you to include loadError event handler in your jqGrid definition. In the way you will see the error responses from the server. See the UPDATED part of the answer. You can load the corresponding demo project from here.

2) You should verify the configuration of the virtual directory where you publish the site. You use [Authorize] attribute. So should disable "Anonymous Authentication" explicitly and enable "Windows Authentication", "Form Authentication" or other Authentication which you plan to use.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thanks for your loadError suggestion, i'll include it. I use [Authorize] on almost every Action in my site, and its working correctly, so I doubt thats the problem... – AJC Aug 13 '11 at 16:52
  • In any way you should know exactly which error you has. In [another answer](http://stackoverflow.com/questions/6960208/jqgrid-server-side-error-message-validation-handling/6969114#6969114) you can find one more variation of the `loadError`. You can include just `alert(jqXHR.responseText)`. The tools like [Fiddler](http://www.fiddler2.com/fiddler2/) of [Firebug](http://getfirebug.com/) can be also very helpful. After you will see **which error you receive from the server** you can make the next step in the correct direction. – Oleg Aug 13 '11 at 17:02
  • I am now able to show several grids, but one in particular gives me an error... I am getting a **500 Internal Server Error** with no more details. Do you know what this might be? – AJC Aug 15 '11 at 16:18
  • @AJC: It is a general exception on the server code. You should verify which value has `includeExceptionDetailInFaults` in the web.config. – Oleg Aug 15 '11 at 16:23
  • You are right... While testing from a browser within the server your loadError handler showed me the error I was having... Thank you. – AJC Aug 15 '11 at 18:48
1

There can be several causes, mainly web server IIS settings - I can guess it's usually security setting so you'd better debug it to find out the real cause. Can you use firebug in firefox and find what the real error message is when server responds to the request? (Firebug -> Console -> appropriate item -> Response) you can start from there.

One thing I would try is to remove Authorize attribute and see if it is working.

OK according to your debugging result, it means that your route had a problem. And I think I found the cause.

 public JsonResult LoadAction(string sidx, string sord, int page, int rows)

But your Url.ActionLink is

   '@Url.Action("LoadAction", "Controller", new { area = "Area" })',

You are not passing your parameter here so that it leads that 500 error. Please fix it and you will get the result.

Tae-Sung Shin
  • 20,215
  • 33
  • 138
  • 240
  • Thanks for responding... I'll check with firefox and see if I can catch the error. What I can't seem to understand is why everywhere else on the site, all other requests are working perfectly. Which is why I thing it migth be an issue or a bug with jqGrid. I could Try and remove [Authorize], but It is necessary to have that since the action can only be executed by authorized users. – AJC Aug 13 '11 at 16:39
  • In the case that other calls are working, it might not be security issue so you can forget it. Let's see what is going on with firebug. – Tae-Sung Shin Aug 13 '11 at 16:44
  • Thanks for your suggestion of using firefox... never used it that way. It helped me identify a different problem, yet related to this issue. – AJC Aug 15 '11 at 14:18
  • Sorry, the error persists... I am now getting a **500 Internal Server Error** with no more details. – AJC Aug 15 '11 at 16:17
  • I think I found the problem. Updated my answer. – Tae-Sung Shin Aug 15 '11 at 16:46
  • Thank you very much for your help... However that was not the problem. I was able to identify the problem thanks to the loadError handler suggested by @Oleg ... Thanks anyway... – AJC Aug 15 '11 at 18:49