数据表显示带有 json 数据的空行

问题描述 投票:0回答:1

我正在使用.Net core(MVC)开发一个项目。我正在尝试发送数据来填充数据表。我在控制器内的代码就像

public JsonResult Get_list(long? subject_id)
{
            DataTable dt = ExamDA.GetTestBySubjectId((long)subject_id, GetString());
            TestInfo test = JsonConvert.DeserializeObject<TestInfo>(json);

            foreach (DataRow dr in dt.Rows)
            {
                test.Data.Add(new TestData()
                {
                    Id = Convert.ToInt32(dr["Id"]),
                    Name = dr["Name"].ToString(),
                    DifficultyLevel = dr["DifficultyLevel"].ToString(),
                    CorrectMarks = Convert.ToDecimal(dr["CorrectMarks"]),
                    WrongMarks = Convert.ToDecimal(dr["WrongMarks"]),
                    strStartDate = dr["StartDate"].ToString(),
                    strEndDate = dr["EndDate"].ToString(),
                });
            }

            //ViewBag.Columns = ti;
            //return Json(test, new Newtonsoft.Json.JsonSerializerSettings());
            return Json(test);
}

列部分中数据表的代码如下:

"columns": [
                { "data": "test", name: 'test', orderable: false, searchable: false }

            ]

但它返回空行。也许序列化存在一些问题。我更改了代码,例如:

columns: [
                { "data": Id },
                { "data": Name },
                { "data": DifficultyLevel },
                { "data": CorrectMarks },
                { "data": WrongMarks },
            
            ]

但是没有显示任何行。

c# asp.net-mvc asp.net-core datatable
1个回答
0
投票
   This code is add in controller
[HttpGet]
public IActionResult GetAll()
{
    List<Company> objCompanyList = _unitOfWork.Company.GetAll().ToList();
    return Json(new { data = objCompanyList });
}
   create js file in www.root
var dataTable;

$(document).ready(function () {
    loadDataTable();
});

function loadDataTable() {
    dataTable = $('#tblData').DataTable({
        "ajax": { url:'/admin/company/getall'},
        "columns": [
            { "data": "name", "width": "15%" },
            { "data": "streetAddress", "width": "15%" },
            { "data": "city", "width": "15%" },
            { "data": "state", "width": "15%" },
            { "data": "phoneNumber", "width": "15%" },
            {
                data: 'id',
                "render": function (data) {
                    return `<div class="w-75 btn-group" role="group">
                     <a href="/admin/company/upsert?id=${data}" class="btn btn-primary mx-2"> <i class="bi bi-pencil-square"></i> Edit</a>               
                     <a onClick=Delete('/admin/company/delete/${data}') class="btn btn-danger mx-2"> <i class="bi bi-trash-fill"></i> Delete</a>
                    </div>`
                },
                "width": "25%"
            }
        ]
    });
}

function Delete(url) {
    Swal.fire({
        title: 'Are you sure?',
        text: "You won't be able to revert this!",
        icon: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: 'Yes, delete it!'
    }).then((result) => {
        if (result.isConfirmed) {
            $.ajax({
                url: url,
                type: 'DELETE',
                success: function (data) {
                    dataTable.ajax.reload();
                    toastr.success(data.message);
                }
            })
        }
    })
}
   this code add in view
<table id="tblData" class="table table-bordered table-striped" style="width:100%">
    <thead>
        <tr>
            <th>Name</th>
            <th>Address</th>
            <th>City</th>
            <th>State</th>
            <th>Phone Number</th>
            <th></th>
        </tr>
    </thead>
</table>
@section Scripts{
    <script src="~/js/company.js"></script>
}
© www.soinside.com 2019 - 2024. All rights reserved.