jQuery Datatable使用asp.net核心中的异步任务方法获取数据

时间:2019-06-03 06:54:44

标签: c# jquery asp.net-core datatables

我想获取数据并显示在asp.net核心的jquery datatable中。 我正在获取找不到错误方法(404),但是我的方法已经在控制器中。我已经检查了其他解决方案,但找不到解决方案。

我的代码如下:

jQuery代码:

$(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"]]
    });
});

从数据表中调用控制器方法:

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

请检查上面的代码。如果我做任何错误或从jquery datatable调用异步任务方法的任何其他方式,请提出建议。

您的帮助将不胜感激。 先感谢您!

1 个答案:

答案 0 :(得分:1)

您的控制器方法是私有的,如果要从控制器类外部访问它,则需要将其公开。