【问题标题】:Display other value than Id in dataTable asp .net core在dataTable asp .net core中显示除Id之外的其他值
【发布时间】:2021-08-15 20:55:59
【问题描述】:

我有 2 个表(客户和约会),约会中有一个外键 customerId。我想显示客户的 customerFirstName 和 customerLastName,但我只能在 loadDataTable ajax 函数中将 customerId 作为数据传递。我不能这样做,因为 customerId 是我的 Appointment 模型中唯一的外键实体。我该怎么做才能在我的 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" },

【问题讨论】:

    标签: sql asp.net-mvc asp.net-core entity-framework-core asp.net-ajax


    【解决方案1】:

    这是一个工作演示:

    约会表:

    约会数据:

    客户表:

    客户数据:

    Appointment.cs:

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

    客户.cs:

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

    动作(GetAll):

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

    结果:

    【讨论】:

    • 我修改了你的一些代码,现在它可以工作了!非常感谢:)
    猜你喜欢
    • 2017-01-04
    • 2019-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-18
    相关资源
    最近更新 更多