通过复选框列表进入查看并拉出了IEnumerable [英] Pass List of Checkboxes into View and Pull out IENumerable

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

问题描述

我有将与用户关联的项目的列表。这是一个一对多的关系。我想传递给视图,使他们能够从没有与之关联的,但(也看到那些已经相关)的人选择项目的完整列表。我要创建这些复选框。然后,我想将选定的被送回控制器关联。我如何可以传递所有的人,包括那些还没有相关的列表,并可靠地传回到有关?

这是我第一次尝试,但很显然因为我立足关通过 AllItems 集合,它没有通过的项目投入,这将不起作用连接到用户本身的项目。

 < D​​IV ID =项目列表>
    @foreach(在Model.AllItems VAR项)
    {
        < D​​IV CLASS =UI字段>
            < D​​IV CLASS =UI切换复选框>
                <输入类型=复选框ID =item-@item.ItemID~~VNAME =项目值=@ item.Active/>
                <标签=item-@item.ItemID> @ item.ItemName< /标签>
            < / DIV>
        < / DIV>
    }
< / DIV>


解决方案

您不能使用的foreach 循环绑定到一个集合。也不应该你可以手动生成你的HTML,在这种情况下,因为未选中复选框不回发是行不通的。让您得到正确的2路模型绑定始终使用强类型的HTML辅助方法。

您还没有显示你的模型是什么,但假设你有一个用户并要选择角色的该用户,然后创建视图模型重新present要在视图中显示哪些

 公共类RoleVM
{
  公众诠释ID {搞定;组; }
  公共字符串名称{;组; }
  公共BOOL IsSelected {搞定;组; }
}
公共类UserVM
{
  公共UserVM()
  {
    角色=新的List< RoleVM>();
  }
  公众诠释ID {搞定;组; }
  公共字符串名称{;组; }
  公开名单< RoleVM>角色{搞定;组; }
}

在GET方法

 公众的ActionResult编辑(INT ID)
{
  UserVM模式=新UserVM();
  //获取你的用户基础上的ID和映射属性视图模型
  //包括填充角色和设置它们的IsSelect财产
  //基于现有的角色
  返回查看(模型);
}

查看

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

然后在POST方法,你的模型将被绑定,您可以检查哪些角色已选定

  [HttpPost]
公众的ActionResult编辑(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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆