how to send all users list to a view in asp.net core 3 mvc from controller
public IActionResult Users()
{
var alluser = _db.Users.ToList();
return View(alluser);
}
Create a class ApplicationDbUser as an example and inherit from IdentityUser to inherit all the properites from the AspNetUsers as follow :
public class ApplicationDbUser : IdentityUser
{
}
then you should use a context as a db (_db in your question) to get the data from the database using EntityFramework as the following code :
public class ApplicationDbContext : IdentityDbContext<ApplicationDbUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
}
now in your controller you can use the code as the following :
private readonly RoleManager<IdentityRole> _roleManager;
private readonly UserManager<ApplicationDbUser> _userManager;
public ExampleController(UserManager<ApplicationDbUser> userManager, RoleManager<IdentityRole> roleManager)
{
_userManager = userManager;
_roleManager = roleManager;
}
public IActionResult Index()
{
var allUsers = _userManager.Users.ToList();
return View(allUsers);
}
You can use it by using @model in the Index view page
in the Index.cshtml you can do the following :
@model IEnumerable<ApplicationDbUser>
<!DOCTYPE html>
<html>
<head>
<title>my page example</title>
</head>
<body>
<div>
<table>
@foreach (var item in Model)
{
<tr>
<td>
@item.Email
</td>
</tr>
}
</table>
</div>
</body>
</html>