将复选框列表传递到视图中并拉出 IEnumerable [英] Pass List of Checkboxes into View and Pull out IEnumerable

查看:22
本文介绍了将复选框列表传递到视图中并拉出 IEnumerable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个将与用户关联的项目列表.这是一对多的关系.我希望将整个项目列表传递到视图中,以便他们可以从尚未与其关联的项目中进行选择(也可以查看已关联的项目).我想从这些创建复选框.然后我想将选定的发送回控制器以进行关联.我怎样才能传入所有这些的列表,包括那些尚未关联的,并可靠地将它们传回以进行关联?

这是我首先尝试的,但很明显这不起作用,因为我将输入基于通过 AllItems 集合传入的项目,该集合与用户本身.

@foreach(Model.AllItems 中的 var 项目){<div class="ui field"><div class="ui 切换复选框"><input type="checkbox" id="item-@item.ItemID" name="Items" value="@item.Active"/><label for="item-@item.ItemID">@item.ItemName</label>

}

解决方案

不能使用 foreach 循环绑定到集合.您也不应该手动生成您的 html,在这种情况下这将不起作用,因为未选中的复选框不会回传.始终使用强类型 html 帮助程序,以便获得正确的 2 路模型绑定.

您没有指明您的模型是什么,但假设您有一个 User 并且想要为该用户选择 Roles,然后创建视图模型来表示您想要的在视图中显示

公共类RoleVM{公共 int ID { 获取;放;}公共字符串名称 { 获取;放;}public bool IsSelected { 获取;放;}}公共类 UserVM{公共用户虚拟机(){Roles = new List();}公共 int ID { 获取;放;}公共字符串名称 { 获取;放;}公共列表<RoleVM>角色{获取;放;}}

在GET方法中

public ActionResult Edit(int ID){UserVM 模型 = new UserVM();//根据 ID 获取用户并将属性映射到视图模型//包括填充角色并设置它们的 IsSelect 属性//基于现有角色返回视图(模型);}

查看

@model UserVM@using(Html.BeginForm()){@Html.HiddenFor(m => m.ID)@Html.DisplayFor(m => m.Name)for(int i = 0; i < Model.Roles.Count; i++){@Html.HiddenFor(m => m.Roles[i].ID)@Html.CheckBoxFor(m => m.Roles[i].IsSelected)@Html.LabelFor(m => m.Roles[i].IsSelected, Model.Roles[i].Name)}<输入类型提交"/>}

然后在post方法中,你的模型将被绑定,你可以查看选择了哪些角色

[HttpPost]公共操作结果编辑(UserVM 模型){//遍历 model.Roles 并检查 IsSelected 属性}

I have a list of items that will be associated to a user. It's a one-to-many relationship. I want the entire list of items passed into the view so that they can choose from ones that are not associated to them yet (and also see those that are already associated). I want to create checkboxes from these. I then want to send the selected ones back into the controller to be associated. How can I pass in the list of all of them, including those that aren't yet associated, and reliably pass them back in to be associated?

Here's what I tried first, but it's clear this won't work as I'm basing the inputs off the items passed in via the AllItems collection, which has no connection to the Items on the user itself.

<div id="item-list">
    @foreach (var item in Model.AllItems)
    {
        <div class="ui field">
            <div class="ui toggle checkbox">
                <input type="checkbox" id="item-@item.ItemID" name="Items" value="@item.Active" />
                <label for="item-@item.ItemID">@item.ItemName</label>
            </div>
        </div>
    }
</div>

解决方案

You cannot bind to a collection using a foreach loop. Nor should you be manually generating your html, which in this case would not work because unchecked checkboxes do not post back. Always use the strongly typed html helpers so you get correct 2-way model binding.

You have not indicated what you models are, but assuming you have a User and want to select Roles for that user, then create view models to represent what you want to display in the view

public class RoleVM
{
  public int ID { get; set; }
  public string Name { get; set; }
  public bool IsSelected { get; set; }
}
public class UserVM
{
  public UserVM()
  {
    Roles = new List<RoleVM>();
  }
  public int ID { get; set; }
  public string Name { get; set; }
  public List<RoleVM> Roles { get; set; }
}

In the GET method

public ActionResult Edit(int ID)
{
  UserVM model = new UserVM();
  // Get you User based on the ID and map properties to the view model
  // including populating the Roles and setting their IsSelect property
  // based on existing roles
  return View(model);
}

View

@model UserVM
@using(Html.BeginForm())
{
  @Html.HiddenFor(m => m.ID)
  @Html.DisplayFor(m => m.Name)
  for(int i = 0; i < Model.Roles.Count; i++)
  {
    @Html.HiddenFor(m => m.Roles[i].ID)
    @Html.CheckBoxFor(m => m.Roles[i].IsSelected)
    @Html.LabelFor(m => m.Roles[i].IsSelected, Model.Roles[i].Name)
  }
  <input type"submit" />
}

Then in the post method, your model will be bound and you can check which roles have been selected

[HttpPost]
public ActionResult Edit(UserVM model)
{
  // Loop through model.Roles and check the IsSelected property
}

这篇关于将复选框列表传递到视图中并拉出 IEnumerable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
C#/.NET最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆