I cannot get serverSide to work, can anyone help!?

I cannot get serverSide to work, can anyone help!?

johnmorsleyjohnmorsley Posts: 4Questions: 1Answers: 0

Hello guys,

I've been trying to get serverSide working all day and I just can't. Can anyone see what I'm doing wrong here?

<table class="table table-striped table-bordered" id="tblUsers">
    <thead>
        <tr class="text-muted">
            <th><small>Unique Identifier</small></th>
        </tr>
    </thead>
</table>
$('#tblUsers').dataTable({
    processing: true,
    serverSide: true,
    ajax: {
        url: '@Url.Action(actionName: "GetPageOfUsers", controllerName: "User")'
    },
    columns: [
        { "data": "UniqueId" }
    ]
});
[HttpGet]
public ActionResult GetPageOfUsers(DataTableParameters parameters)
{
    var areaId = GetAreaId();

    if (!areaId.HasValue) return null;

    var users = GetUsers(areaId: areaId.Value, skip: 0, take: 1);

    return Json(
        new
            {
                draw = parameters.Draw,
                recordsTotal = GetUserCount(areaId.Value),
                recordsFiltered = GetUserCount(areaId.Value, parameters.Search),
                data = JsonConvert.SerializeObject(users, Formatting.None)
            },
        JsonRequestBehavior.AllowGet);
}

Thanks :)

This question has accepted answers - jump to:

Answers

  • rob morningrob morning Posts: 30Questions: 7Answers: 1

    What does the '@Url.Action(..' get expanded to ? Forgive my ignorance - I use grails and server side processing without any problems ..

  • matthttammatthttam Posts: 40Questions: 2Answers: 0

    What language is this? I recently got server-side working with ruby on rails and it was not easy. Took a couple days of playing around then refactoring everything so I didn't have a giant block of code in my controller.

    Server-Side means that your server side script has to handle sorting, filtering, and pagination by itself. Datatables will send json data that needs to be parsed, then a query should happen based off that parsing and that data returned. This is only needed if you are dealing with HUGE amounts of data. Some of my tables have 30 thousand entries so this was a must for me.

  • allanallan Posts: 63,687Questions: 1Answers: 10,500 Site admin
    Answer ✓

    @johnmorsley - What JSON is your script currently responding with?

    Allan

  • johnmorsleyjohnmorsley Posts: 4Questions: 1Answers: 0

    Hi guys,

    Thanks for your responses! :)

    Rob

    @Url.Action(actionName: "GetPageOfUsers", controllerName: "User")
    

    Resolves to

    /User/GetPageOfUsers
    

    Which is correct. This part works as this corresponds to the GetPageOfUsers action on the User controller. I can verify this is working correctly and returns JSON.

    matthttam

    This is ASP.NET MVC. Yes, my results are potentially huge. This was for a User table with thousands of users. So server side is perfect!

    allan

    The JSON 'looks' OK, but maybe one of you can see an issue with it, so here it is...

    {"draw":"1","recordsTotal":49500,"recordsFiltered":49500,"data":"[{\"UniqueId\":\"994631773648\"}]"}
    

    This is a very cut down version with the server side code returning 1 row with 1 column!

    Any further help is much appreciated...

  • allanallan Posts: 63,687Questions: 1Answers: 10,500 Site admin
    Answer ✓

    "data":"[{\"UniqueId\":\"994631773648\"}]"}

    That is the issue in the JSON. It should be:

    "data":[{"UniqueId":"994631773648"}]}
    

    i.e. data should be an array, not a string.

    Try just data = users in the output.

    Allan

  • johnmorsleyjohnmorsley Posts: 4Questions: 1Answers: 0

    Thanks Allan, I'll give that a try right now... :)

  • johnmorsleyjohnmorsley Posts: 4Questions: 1Answers: 0

    Tried that Allan and it worked! So can't thank you enough :)

This discussion has been closed.