在 dataTable asp .net core 中显示除 Id 以外的其他值
Display other value than Id in dataTable asp .net core
我有 2 个表(客户和约会),约会中的外键为 customerId。我想显示客户的 customerFirstName 和 customerLastName,但我只能将 customerId 作为数据传递到我的 loadDataTable ajax 函数中。我不能这样做,因为 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" },
这是一个工作演示:
约会table:
预约数据:
客户Table:
客户数据:
Appointment.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; }
}
动作(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>
}
结果:
我有 2 个表(客户和约会),约会中的外键为 customerId。我想显示客户的 customerFirstName 和 customerLastName,但我只能将 customerId 作为数据传递到我的 loadDataTable ajax 函数中。我不能这样做,因为 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" },
这是一个工作演示:
约会table:
预约数据:
客户Table:
客户数据:
Appointment.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; }
}
动作(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>
}
结果: