Home > Enterprise >  Cant get model to return users and roles
Cant get model to return users and roles

Time:01-30

I have been racking my brain over this for like a week straight and cant find an answer or get help. I have found every tutorial possible, and every Microsoft article related to this and every StackOverflow article. I am very well aware what the MVC is, and why null values are returned and what the Null Exception error message means and why you get one. What I cannot figure out is I create a brand new project, and follow multiple tutorials, and even match my code to Microsoft tutorial here and it nearly matches up identically. Why is my model always null. Clearly when I log in or register someone it doesnt pull these null... Am I missing something? This seems really straight forward. My GET always returns null. Am I misunderstanding something? I feel like I am doing something wrong here...

Model

 public class UserRolesViewModel
    {
        public string? UserId { get; set; }
        public string? FirstName { get; set; }
        public string? LastName { get; set; }
        public string? UserName { get; set; }
        public string? Email { get; set; }
        public IEnumerable<string>? Roles { get; set; }
    }

Controller

public class UserRolesController : Controller
{
    private readonly UserManager<ApplicationUser> _userManager;
    private readonly RoleManager<IdentityRole> _roleManager;
    public UserRolesController(UserManager<ApplicationUser> userManager, RoleManager<IdentityRole> roleManager)
    {
        _roleManager = roleManager;
        _userManager = userManager;
    }
    public async Task<IActionResult> Administration()
    {
        var users = await _userManager.Users.ToListAsync();
        var userRolesViewModel = new List<UserRolesViewModel>();
        foreach (ApplicationUser user in users)
        {
            var thisViewModel = new UserRolesViewModel();
            thisViewModel.UserId = user.Id;
            thisViewModel.Email = user.Email;
            thisViewModel.FirstName = user.FirstName;
            thisViewModel.LastName = user.LastName;
            thisViewModel.Roles = await GetUserRoles(user);
            userRolesViewModel.Add(thisViewModel);
        }
        return View(userRolesViewModel);
    }
    private async Task<List<string>> GetUserRoles(ApplicationUser user)
    {
        return new List<string>(await _userManager.GetRolesAsync(user));
    }

View

@model List<UserRolesViewModel>
@using RabbitMSP.Models
@{
    ViewData["Title"] = "Administration";
    Layout = "~/Views/Shared/_Layout.cshtml";

}
<h1>User Roles</h1>
<table >
    <thead>
        <tr>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Email</th>
            <th>Roles</th>
            <th>Action</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var user in Model)
        {
        <tr>
            <td>@user.FirstName</td>
            <td>@user.LastName</td>
           @* <td>@user.Email</td>
            <td>@string.Join(" , ", user.Roles.ToList())</td>*@
 @*           <td>
                <a  asp-controller="UserRoles" asp-action="Manage" asp-route-userId="@user.UserId">Manage Roles</a>
            </td>*@
        </tr>
        }

    </tbody>
</table>

CodePudding user response:

I fixed this with : ApplicationUser in the model. This seems to be when I convert IdentityUser to ApplicationUser I need to instantiate that applicationuser method.

public class UserRolesViewModel : ApplicationUser
    {
        public string? UserId { get; set; }
        public string? FirstName { get; set; }
        public string? LastName { get; set; }
        public string? UserName { get; set; }
        public string? Email { get; set; }
        public IEnumerable<string>? Roles { get; set; }
    }

CodePudding user response:

I had same problem.

Try to debug this line by adding checkpoint on it, also in your view add @if(Model != null && Model.UserRolesViewModel.Count() > 0) { Your code } else{ Nothing here }

I hope this will provide you some help. Thanks!

  •  Tags:  
  • Related