I would like to delete a user with the following code:
private readonly UserManager<IdentityUser> userManager;
public UserPage(UserManager<IdentityUser> userManager)
{
this.userManager = userManager;
}
public async Task<IActionResult> OnPostDelete(string id)
{
IdentityUser user = await userManager.FindByIdAsync(id);
if(user != null)
{
var result = await userManager.DeleteAsync(user);
if (result.Succeeded)
{
return RedirectToAction("UserPage");
}
else
{
foreach (IdentityError error in result.Errors)
ModelState.AddModelError("", error.Description);
}
}
return Page();
}
And this is my cshtml file where I get the error : 'Foreach statement cannot operate on variables of type UserPage because Userpage does not contain a public instance or extension definition for 'GetEnumerator''
<table >
<tr><th>ID</th><th>Name</th><th>Email</th></tr>
@foreach (IdentityUser user in *Model*)
{
<tr>
<td>@user.Id</td>
<td>@user.UserName</td>
<td>@user.Email</td>
<td>
<form asp-page-handler="Delete" asp-route-id="@user.Id" method="post">
<button type="submit" >
Delete
</button>
</form>
</td>
</tr>
}
How Could I solve this? Thanks in advance.
CodePudding user response:
'UserPage' does not contain a public instance or extension definition for 'GetEnumerator'
The foreach statement is used to iterate through an object collection that implements the System.Collections.IEnumerable or System.Collections.Generic.IEnumerable interface.
So the error is because the type of *Model*
is not correct.You need to change it to an object collection that implements the System.Collections.IEnumerable or System.Collections.Generic.IEnumerable interface.