【问题标题】:Passing an array of ids from view pages to controller将一组 id 从视图页面传递给控制器
【发布时间】: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-forname 属性?更推荐使用asp-for 作为输入标签助手。

标签: c# asp.net-mvc asp.net-core razor


【解决方案1】:

Asp.Net MVC 中的模型绑定是由 html 元素的name 属性完成的。因此,将name 属性添加到input,其值为userIds。因此,同名的多个元素将绑定到同名的单个参数,从而生成一个列表:

<input name="userIds" type="checkbox" id="@item.Id" value="@item.Id" />

【讨论】:

    【解决方案2】:

    您需要在您的 Html 中通过给它们一个“index”来表明这些项目属于一个列表,并且通过这样做model binding 将能够识别您正在创建一个列表,并使用asp net core 的标签助手,它应该生成绑定所需的准确 html:

    <form asp-controller="Account" asp-action="Remove" method="post">
    
                @for (int i = 0; i < Model.Users.Count; i++)
                {
                <div>
                    <label> @Model.Users[i].FirstName @Model.Users[i].LastName</label>
                    <input type="checkbox" asp-for="@Model.Users[i].Id" />
                </div>
                }
    
    
            <input type="submit" value=" Remove User" /><br>
    
        </form>
    

    我建议您查看标签助手,因为它们可以使您的标记更具可读性并且不易出错,因为它们会为您生成很多东西。

    您还可以编写自己的自定义代码,让生活变得非常轻松!

    一旦您尝试了上述方法,我建议您在浏览器调试器中检查生成的 html,以查看它创建的 html 并了解绑定的工作原理。

    【讨论】:

      【解决方案3】:

      您需要使用name 属性将选定的 id 传递给具有相应参数名称的操作。

      查看:

      @foreach (var item in Model.Users)
      {
          <div>
              <label>@item.FirstName @item.LastName</label>
              <input type="checkbox" name="userIds" id="@item.Id" value="@item.Id" />
          </div>
      }
      

      行动:

      [HttpPost]
      public IActionResult Remove(int[] userIds)
      

      【讨论】:

        【解决方案4】:

        谢谢大家,就像将nameasp-for 属性添加到我的输入标签以确保数据绑定一样简单。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-03-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多