0

I am working on a small project and trying to display the fullname (which added by me on in ApplicationUser Model inhireted from IdentityUser) and below the Model:

public class ApplicationUser : IdentityUser 
{
    public int PersonnalNumber { get; set; }
    public string FullName { get; set; }
}

here is SystemDetails Model have relationship with Application User:

public class SystemDetails
{
    [Key]
    public int Id { get; set; }

    [Required]
    [Display(Name = "System Name")]
    public string SystemName { get; set; }
    [Required]
    [Display(Name = "Admin Name")]
    public string SystemAdminId { get; set; }
    public string ServerNames { get; set; }

    [ForeignKey("SystemAdminId")]
    public virtual ApplicationUser ApplicationUser { get; set; }
}

and here is the Index View:

@if (Model.Count() > 0)
    {
        <table class=" table table-striped border">
            <tr class="table-secondary">

                <th>
                    @Html.DisplayNameFor(m => m.SystemName)
                </th>
                <th>
                    @Html.DisplayNameFor(m => m.SystemAdminId)
                </th>

                <th></th>
                <th></th>
            </tr>
            @foreach (var item in Model)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(m => item.SystemName)
                    </td>

                    <td>
                        @Html.DisplayFor(m => item.ApplicationUser.FullName)
                    </td>


                    <td>
                        <partial name="_TableButtonPartial" model="@item.Id" />
                    </td>
                </tr>
            }

Everything working fine except that FullName is not displayed on index page, I can display SystemAdminId which is the registered user Id but when i try to display the FullName using Applicationuser.FullName it is not displaying any thing! it is saved correctly on the database when I create record on SystemDetails Controller as below:

SystemAdminId

Here is create action:

[HttpPost,ActionName("Create")]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> CreatePost()
    {
        List<ApplicationUser> userList = new List<ApplicationUser>();


        if (!ModelState.IsValid)
        {
            return NotFound();
        }
        userList = await (from user in _db.ApplicationUser select user).ToListAsync();
        ViewBag.usersList = userList;


        _db.SystemDetails.Add(SystemDetails);
        await _db.SaveChangesAsync();
        return RedirectToAction(nameof(Index));
    }

Appreciating any help

omarkkk
  • 11
  • 3
  • Look [here](https://stackoverflow.com/questions/36641338/how-to-get-current-user-in-asp-net-core), may be it will help you out. – Rafaqat Ali Dec 15 '19 at 09:40

1 Answers1

0

In controller , when querying the SystemDetails list you should load the related ApplicationUser :

using Microsoft.EntityFrameworkCore;

var result = _dbContext.SystemDetails.Include(f => f.ApplicationUser).ToList();
Nan Yu
  • 21,285
  • 5
  • 39
  • 110