Search code examples
sql-serverasp.net-coreasp.net-core-mvcidentityasp.net-core-identity

How get User Information from SQL Server with identity


I'm using ASP.NET Core MVC. I want to get User Information from SQL Server with Identity.

This is my AdminController:

private ApplicationDbContext _application;
public AdminController (ApplicationDbContext application)
{
    _application = application;
}

public IActionResult ListUsers()
{
    return View(_application.Users.ToList());
}

My View:

{
    <td>@Html.DisplayNameFor(model => model.Id)</td>
}

but it shows me that Error.

"InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'System.Collections.Generic.List1[Microsoft.AspNetCore.Identity.IdentityUser]', but this ViewDataDictionary instance requires a model item of type 'System.Collections.Generic.IEnumerable1[WebAppMVC.ApplicationUser]'.


Solution

  • Here is a working demo , please check the difference on yours and make modification :

    DbContext

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }
    }
    

    Controller

    public class HomeController : Controller
    {
        private readonly ApplicationDbContext _dbcontext;
    
        public HomeController( ApplicationDbContext dbcontext)
        {
            _dbcontext = dbcontext;
        }
    
        public IActionResult ListUsers()
        {
            return View(_dbcontext.Users.ToList());
        }
    }
    

    View

    @model IEnumerable<MVCIdentityDemo3_1.Models.ApplicationUser>
    
    <table class="table">
      <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Id)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.UserName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.FirstName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.LastName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Email)
            </th>
            <th></th>
        </tr>
      </thead>
      <tbody>
        @foreach (var item in Model)
        {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Id)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.UserName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.FirstName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.LastName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Email)
            </td>
            <td>
                <a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
                <a asp-action="Details" asp-route-id="@item.Id">Details</a> |
                <a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
            </td>
        </tr>
        }
      </tbody>
    </table>