【发布时间】:2021-02-23 12:15:24
【问题描述】:
我在 Datatable 中显示 ClientName 时遇到问题。在我的数据表中包含 ClientName 后,它不会显示。我试图找出问题所在,但我没有看到错误。在我的console.log 中,我没有收到任何错误,这让我有点困惑,很难找出问题所在。
public IActionResult GetAll()
{
var userList = _db.ApplicationUsers.Include(u => u.Client).ToList();
var userRole = _db.UserRoles.ToList();
var roles = _db.Roles.ToList();
foreach(var user in userList)
{
var roleId = userRole.FirstOrDefault(u => u.UserId == user.Id).RoleId;
user.Role = roles.FirstOrDefault(u => u.Id == roleId).Name;
if(user.ClientId == null)
{
user.Client = new Client()
{
Name = ""
};
}
}
return Json(new { data = userList });
}
@{
ViewData["Title"] = "Index";
Layout = "~/Views/Shared/AdminLTE/_Layout.cshtml";
}
<br />
<div class="row">
<div class="col-6">
<h2 class="text-primary">Lista Usera</h2>
</div>
<div class="col-6 text-right">
<a class="btn btn-primary" asp-area="Identity" asp-page="/Account/Register">
<i class="fas fa-plus"></i> Kreiraj novog user</a>
</div>
</div>
<br />
<div class="p-5 border rounded">
<table id="tblData" class="table table-striped table-bordered" style="width:100%">
<thead class="thead-dark">
<tr class="table-info">
<th>Ime i Prezime</th>
<th>Email</th>
<th>Broj Telefona</th>
<th>User Rolna</th>
<th>Klijent</th>
<th></th>
</tr>
</thead>
</table>
</div>
@section Scripts{
<script src="~/js/user.js"></script>
}
var dataTable;
$(document).ready(function () {
loadDataTable();
});
function loadDataTable() {
dataTable = $('#tblData').DataTable({
"ajax": {
"url": "/Admin/User/GetAll"
},
"columns": [
{ "data": "name", "width": "15%" },
{ "data": "email", "width": "15%" },
{ "data": "phoneNumber", "width": "15%" },
{ "data": "role", "width": "15%" },
{ "data": "client.name", "width": "10%" },
{
"data": {
id: "id", lockoutEnd: "lockoutEnd"
},
"render": function (data) {
var today = new Date().getTime();
var lockout = new Date(data.lockoutEnd).getTime();
if (lockout > today) {
//user is currently locked
return `
<div class="text-center">
<a onclick=LockUnlock('${data.id}') class="btn btn-danger text-white" style="cursor:pointer; width:100px;">
<i class="fas fa-lock-open"></i> Unlock
</a>
</div>
`;
}
else {
return `
<div class="text-center">
<a onclick=LockUnlock('${data.id}') class="btn btn-success text-white" style="cursor:pointer; width:100px;">
<i class="fas fa-lock"></i> Lock
</a>
</div>
`;
}
}, "width": "20%"
}
]
});
}
function LockUnlock(id) {
$.ajax({
type: "POST",
url: '/Admin/User/LockUnlock',
data: JSON.stringify(id),
contentType: "application/json",
success: function (data) {
if (data.success) {
toastr.success(data.message);
dataTable.ajax.reload();
}
else {
toastr.error(data.message);
}
}
});
}
我尝试更改我的 user.js
{ "data": "client.name", "width": "10%" },
和
{ "data": "Client.name", "width": "10%" },
和
{ "data": "Client.Name", "width": "10%" },
但我仍然面临同样的问题。 有谁知道错误是什么?我做错了什么?
更新
ApplicationUser.cs
public class ApplicationUser : IdentityUser
{
[Required]
[Display(Name = "Ime")]
public string Name { get; set; }
[Required]
[Display(Name = "Adresa")]
public string StreetAddress { get; set; }
[Required]
[Display(Name = "Grad")]
public string City { get; set; }
[Required]
[Display(Name = "Postanski broj")]
public string PostalCode { get; set; }
public int? ClientId { get; set; }
[ForeignKey("ClientId")]
public Client Client { get; set; }
[NotMapped]
public string Role { get; set; }
}
客户端.cs
public class Client
{
[Key]
public int Id { get; set; }
[Required]
[Display(Name = "Ime")]
public string Name { get; set; }
[Required]
[Display(Name = "Adresa")]
public string StreetAddress { get; set; }
[Required]
[Display(Name = "Grad")]
public string City { get; set; }
[Required]
[Display(Name = "Broj telefona")]
public string PhoneNumber { get; set; }
public bool isAuthorizedClient { get; set; }
}
【问题讨论】:
-
你确定你的 json 用户对象有属性 Client 女巫有属性名 'user.Client.Name' 吗?
-
是的,我确定。我更新了我的问题。
-
您的 C# 模型看起来不错,但请检查什么 json 结构返回 GetAll 方法。它应该像 [ ["StreetAddress ":"Some address",..
.., "Client" :["Name":"Client name"] ], ] -
我检查并看到它现在返回 Client.Name。我尝试调试应用程序并检查发生了什么
标签: c# datatables asp.net-core-mvc