【发布时间】:2019-08-02 13:04:16
【问题描述】:
我发现 ID 没有从剃刀页面传递到控制器。所以我的int[] userIds 是空的,但我不知道为什么或如何解决它。请帮忙。
下面是我的代码。
账户管理员:
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");
}
查看:
@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>
视图模型:
namespace WebApplication1.ViewModels
{
public class RemoveViewModel
{
public List<ApplicationUser> Users { get; set; }
public RemoveViewModel(List<ApplicationUser> users)
{
Users = users;
}
}
}
【问题讨论】:
-
为什么不在您的复选框中使用
asp-for或name属性?更推荐使用asp-for作为输入标签助手。
标签: c# asp.net-mvc asp.net-core razor