How to pass a parameter through DataTable AjaxSource?

We are trying to populate a JQuery DataTable by pulling data from a Sql server. So far, I am able to call the C# function without a parameter, but when I try to pass a parameter the method call fails saying, "val.fn.apply is not a function". How can I pass a parameter to the C# function?

Here is the Jquery function to create the datatable. As mentioned it works without a parameter (staffId), but fails with fnServerParams property.

    function getData(staffId) {
        processing: true,
        ajaxSource: '@Url.Action("GetStaffPositions", "UserAccount")',
        fnServerParams: staffId,
        destroy: true,
        paging: false,
        filter: false,
        autoWidth: false,
        sort: false

Here is the call to the Javascript function. It is inside a loop.

<a href="#" onclick="getData('@(item.StaffID)')" style="cursor: pointer;">Select</a>

Here is the C# ActionResult method.

    public ActionResult GetStaffPositions(int staffId)
            // get staff positions for the given staffId from new view
            List<StaffPositionsTitleView> staffPositionsTitleViewList  = _service.GetStaffPositionsTitleViewList(staffId);
            List<StaffPosition> staffPositionList = new List<StaffPosition>();

            // map new view to List<StaffPosition>
            staffPositionList = AutoMapper.Mapper.Map<List<StaffPositionsTitleView>, List<StaffPosition>>(staffPositionsTitleViewList);

            return Json(new
                data = staffPositionList.Select(x => new object[] { x.StaffId, x.Title })
            }, JsonRequestBehavior.AllowGet);
        catch (Exception ex)
            throw ex;

Answers 1

  • Found my answer. Changed this fnServerParams: staffId, to this

    fnServerParams: function (d) { d.push( { "name": "StaffId", "value": staffId } ) },

Related Articles