Dhrutika Rathod
Dhrutika Rathod

Reputation: 560

Jquery Datatable fetch data with async task method in asp.net core

I want to fetch data and display in jquery datatable in asp.net core. I am getting error method not found (404), but my method is already in the controller. I have checked the other solutions but I can not find the solution.

My code is as follows:

Jquery Code:

$(document).ready(function() {
    $("#kt_table_1").DataTable({
    "oLanguage": {
        "sZeroRecords": "No records to display",
    },
    "bProcessing": true,
    "bServerSide": true,
    "searching": false,
    "bDestroy": true,
    "bAutoWidth": false,
    "lengthMenu": [[10, 20, 30], [10, 20, 30]],
    "sAjaxSource": "@Url.Action("GetUsersList","UserProfile")",
    "fnServerParams": function (aoData) {
        aoData.push({ "name": "searchdata", "value": "" });
        perm = aoData;
    },
    "fnServerData": function (sSource, aoData, fnCallback, oSettings) {
        oSettings.jqXHR = $.ajax({
            "dataType": 'json',
            "type": "POST",
            "url": sSource,
            "data": aoData,
            "success": fnCallback,
            "timeOut": 10000
        });
    },
    "sPaginationType": "full_numbers",
    "bDeferRender": true,
    "aoColumns":
    [
        {
            "className": 'details-control',
            "orderable": false,
            "data": null,
            "defaultContent": ''
        },
        { "sName": "UserId", "bSearchable": false, "bSortable": false, "width": "25px" },
        { "sName": "UserName", "bSearchable": true, "bSortable": true },
        { "sName": "UserEmail", "bSearchable": true, "bSortable": true },
        { "sName": "UserMobileNo", "bSearchable": true, "bSortable": true },
        { "sName": "UserStatus", "bSearchable": true, "bSortable": true },
       // { "sName": "Action", "bSearchable": false, "bSortable": false, "className": "icn" }
    ],
    "sAjaxSource": "@Url.Action("GetUsersList","UserProfile")",
    "order": [[2, "desc"]]
    });
});

Controller Method call from the datatable:

   private async Task<IActionResult> GetUsersList(string sEcho, int iDisplayStart, int iDisplayLength, int iSortCol_0, string sSortDir_0, string sColumns, string searchdata)
    {
        JQueryDataTableParamModel parm = new JQueryDataTableParamModel();
        parm.sEcho = sEcho;
        parm.iDisplayStart = iDisplayStart;
        parm.iDisplayLength = iDisplayLength;
        parm.sSortDir_0 = sSortDir_0;
        var sColumnsArray = sColumns.Split(',');
        string sortColumn = "";
        if (sColumnsArray.Length > 0 && sColumnsArray.Length >= iSortCol_0)
            sortColumn = Convert.ToString(sColumnsArray[iSortCol_0]);
        try
        {
            IEnumerable<string[]> Adminusers = Enumerable.Empty<string[]>();
            if (!String.IsNullOrEmpty(parm.sSearch))
            {
                parm.sSearch = parm.sSearch.Replace("'", "''");
            }
            ApiClient apiClient = new ApiClient(apiUrl);
            var requestUrl = apiClient.CreateRequestUri(string.Format(System.Globalization.CultureInfo.InvariantCulture,"UsersApi/GetUsersList"));
            List<UserProfile> obj= await Client.GetAsync<List<UserProfile>>(requestUrl);
            List<UserProfile> lstusersModel = new List<UserProfile>();
            foreach (var item in obj)
            {
                UserProfile objusersModel = new UserProfile();
                objusersModel.UserId = item.UserId;
                objusersModel.UserName = item.UserName.ToString();
                objusersModel.UserEmail = item.UserEmail.ToString();
                objusersModel.UserMobileNo = item.UserMobileNo.ToString();
                objusersModel.UserStatus = item.UserStatus;
                lstusersModel.Add(objusersModel);
            }
            Adminusers = from c in lstusersModel
                         select new string[] {
                                    c.UserId.ToString(),
                                    "<input type='checkbox' name='checkexportId' id='"+ c.UserId +"' Class='checkexportclass' />",
                                    c.UserName.ToString(),
                                    c.UserEmail.ToString(),
                                    c.UserMobileNo.ToString(),
                                    c.UserStatus.ToString(),

            };
            int noOfRecoreds = 10;
            return Json(new
            {
                sEcho = parm.sEcho,
                iTotalRecords = obj.Count,
                iTotalDisplayRecords = noOfRecoreds,
                aaData = Adminusers
            });
        }
        catch
        {
            throw;
        }
    }

Please check above code. Please suggest me if I am doing any mistake or any other way to call async task method from jquery datatable.

Your help will be really appreciated. Thank you in advance!

Upvotes: 0

Views: 544

Answers (1)

Oscar
Oscar

Reputation: 13990

Your controller method is private, you need to make it public if you want to access it from outside the controller class.

Upvotes: 1

Related Questions