在 dataTable asp .net core

问题描述

我有 2 个表(客户和约会),约会中的外键是 customerId。我想显示客户的 customerFirstName 和 customerLastName,但我只能在 loadDataTable ajax 函数中将 customerId 作为数据传递。我不能这样做,因为 customerId 是我的约会模型中唯一的外键实体。我该怎么做才能在我的 ajax 函数中传递 customerFirstName 和 customerLastName 数据? My appointment table

var dataTable;

$(document).ready(function () {
    loadDataTable();
});
function loadDataTable() {
    dataTable = $('#DT_load').DataTable({
"ajax": {
        "url": "/Appointment/getall/","type": "GET","datatype": "json"
    },"columns": [
        { "data": "customerId","width": "auto" },

解决方法

这是一个工作演示:

预约表:

enter image description here

约会数据:

enter image description here

客户表:

enter image description here

客户数据:

enter image description here

约会.cs:

public class Appointment
    {
        public int AppointmentId { get; set; }
        [ForeignKey("CustomerId")]
        public Customer Customer { get; set; }
    }

Customer.cs:

public class Customer
    {
        public int CustomerId { get; set; }
        public string CustomerFirstName { get; set; }
        public string CustomerLastName { get; set; }

    }

操作(获取全部):

public async Task<IActionResult> GetAllAsync()
        {
            List<Appointment> l=await _context.Appointment.Include(a => a.Customer).ToListAsync();
            return Json(l);
        }

查看:

<table id="DT_load">
    <thead>
        <tr>
            <td>AppointmentId</td>
            <td>CustomerFirstName</td>
            <td>CustomerLastName</td>

        </tr>
    </thead>
    <tbody>
    </tbody>

</table>
@section scripts{
    <script src="~/lib/jquery/dist/jquery.min.js"></script>
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.21/css/jquery.dataTables.css">

    <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.js"></script>
<script>
    var dataTable;

    $(document).ready(function () {
        loadDataTable();
    });
    function loadDataTable() {
        dataTable = $('#DT_load').DataTable({
            "ajax": {
                "url": "/Appointment/getall","dataSrc": ""
            },"columns": [
                { "data": "appointmentId" },{ "data": "customer.customerFirstName" },{ "data": "customer.customerLastName"},]
        });
    }
</script>
}

结果: enter image description here