为什么ClientName不能在Datatable中显示

问题描述

我无法在 ClientName显示 Datatable。在我的数据表中包含 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> &nbsp; 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",{ "data": "phoneNumber",{ "data": "role",{ "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);
            }
        }
    });

}

enter image description here

我尝试更改我的 user.js

 { "data": "client.name",

 { "data": "Client.name",

 { "data": "Client.Name",

但我仍然面临同样的问题。 有谁知道错误是?我做错了什么?

更新

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

Client.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; }

    }

解决方法

我注意到问题来自于我的 coockies。清除 coockies 和浏览器数据后,它在数据表中成功呈现 ClientName