I discovered that the ids are not getting passed through from the razor page to the controller. So my int[] userIds
is empty but I don't know why or how to fix it. Please help.
Below is my code.
Account Controller:
public class AccountController : Controller
{
private readonly UserManager<ApplicationUser> _userManager;
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly RoleManager<Role> _roleManager;
public AccountController(UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signInManager, RoleManager<Role> roleManager)
{
_userManager = userManager;
_signInManager = signInManager;
_roleManager = roleManager;
}
public IActionResult Remove()
{
var users = _userManager.Users.OrderBy(o=> o.LastName).ToList();
RemoveViewModel removeViewModel = new RemoveViewModel(users);
return View(removeViewModel);
}
[HttpPost]
public IActionResult Remove(int[] userIds)
{
if (userIds == null || userIds.Length==0)
{
throw new System.ArgumentException("Array is empty", "original");
}
return Redirect("/Home/Index");
}
view:
@model WebApplication1.ViewModels.RemoveViewModel
<html>
<body>
<form asp-controller="Account" asp-action="Remove" method="post">
@foreach (var item in Model.Users)
{
<div>
<label> @item.FirstName @item.LastName</label>
<input type="checkbox" id="@item.Id" value="@item.Id" />
</div>
}
<input type="submit" value=" Remove User" /><br>
</form>
</body>
</html>
ViewModel:
namespace WebApplication1.ViewModels
{
public class RemoveViewModel
{
public List<ApplicationUser> Users { get; set; }
public RemoveViewModel(List<ApplicationUser> users)
{
Users = users;
}
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…