我来自数据库的数据未显示在jQuery数据表中

时间:2019-08-21 11:37:36

标签: javascript c# jquery asp.net-mvc datatables

我想显示中数据库中的数据,但是不显示数据。它只是显示了一个简单的列表,我在下面发布了一个列表,我尝试了很多发现相关问题的东西,但是它们都在PHP中,我不了解,因为我对PHP一无所知。

jQuery ajax

$(document).ready(function () {
    $("#demoGrid").DataTable({
        "processing": true,
        "serverSide": true,
        "info": true,
        "stateSave": true,
        "lengthMenu": [[10, 20, 50, -1], [10, 20, 50, "All"]],
        "ajax": {
            "url": "/Login/Display",
            "type": "GET"
        },
        "columns": [
            { "data": "EmployeeId", "orderable": true },
            { "data": "Name", "orderable": true },
            { "data": "Position", "orderable": true },
            { "data": "Office", "orderable": true },
            { "data": "Salary", "orderable": true },
        ],
        "order": [[0, "asc"]]
    });
});

控制器

public ActionResult Display()
        {
            List<Empdetail> list = new List<Empdetail>();
            DataSet ds = new DataSet();
            Connection.connection con = new Connection.connection();
            ds = con.mydata();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                list.Add(new Empdetail
                {
                    EmployeeId = (int)(dr["EmployeeId"]),
                    Name = (string)dr["Name"],
                    Position = (string)dr["Position"],
                    Office = (string)(dr["Office"]),
                    Salary = (int)(dr["Salary"]),
                });
            }
            return Json(list, JsonRequestBehavior.AllowGet);
        }

型号:

public class Empdetail
    {
        public int EmployeeId { get; set; }
        public string Name { get; set; }
        public string Position { get; set; }
        public string Office { get; set; }
        public int Salary { get; set; }
    }

输出

[{"EmployeeId":1,"Name":"Imran Khan","Position":"PM","Office":"PM House","Salary":1000000}]

1 个答案:

答案 0 :(得分:0)

您需要从Controller以Datatable的特定格式返回数据,如下所示:

    return Json(new
    {
        draw = "1",
        recordsTotal = list.count,
        recordsFiltered = list.count,
        data = List
    }, JsonRequestBehavior.AllowGet);

有关更多参考信息,请检查here