剃刀中的多个复选框(使用 foreach) [英] Multiple checkboxes in razor (using foreach)

查看:19
本文介绍了剃刀中的多个复选框(使用 foreach)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我找不到解决办法.我正在使用 Razor,它是我的 VieModel 类.

I have a problem and I can't find solution. I'm using Razor and it is my VieModel class.

public class GroupToExport
{
    public GroupToExport()
    {
        ToExport = false;
    }

    [DisplayName("Export")]
    public bool ToExport { get; set; }
    public Group Group { get; set; }

}

public class GroupsToExport
{
    public GroupsToExport()
    {
        //refill list
    }

    public List<GroupToExport> ExportingGroups { get; set; }
}

查看:

@using (Html.BeginForm("Export", "ElmahGroup", FormMethod.Post, new { id = "toExportForm" }))
{
//some divs
    <input type="submit" id="js-export-submit" value="Export" />
 @foreach (var item in Model.ExportingGroups)
                {
                    <tr>
                        <td class="js-export-checkbox">
                            @Html.CheckBoxFor(modelItem => item.ToExport)
                        </td>
                    </tr>
                }
//some divs
}

控制器:

public ActionResult Export(GroupsToExport model)
    {
        var groupsToExport = model.ExportingGroups.Where(x => x.ToExport).Select(x => x);
        throw new System.NotImplementedException();
    }

提交ToExport"后,在Controller中,每个组总是有值false".即使检查了所有组.

After submit "ToExport", in Controller, every group always has value 'false'. Even if all groups are checked.

有人可以帮我吗?我做错了什么?

Can somebody help me? What I'm doing wrong?

推荐答案

您不能使用 foreach 循环为集合生成控件.您为每个复选框(以及关联的隐藏输入)生成的 html 是 <input type="checkbox" name="item.ToExport" .../>.您的模型不包含名为 item 的属性.

You cannot use a foreach loop to generate controls for a collection. The html you're generating for each checkbox (and for the associated hidden input) is <input type="checkbox" name="item.ToExport" .../>. Your model does not contain a property which is named item.

使用 for 循环

@for(int i = 0; i < Model.ExportingGroups.Count; i++)
{
  <tr>
    <td class="js-export-checkbox">
      @Html.CheckBoxFor(m => m.ExportingGroups[i].ToExport)
    </td>
  </tr>
}

现在您的 HTML 将是

Now your HTML will be

<input name="ExportingGroups[0].ToExport" .../>
<input name="ExportingGroups[1].ToExport" .../>

等等.这将正确绑定到您的模型

etc. which will correctly bind to your model

编辑

或者,您可以为 GroupToExport 的 typeof 使用自定义 EditorTemplate.创建局部视图 /Views/Shared/EditorTemplates/GroupToExport.cshtml

Alternatively you can use a custom EditorTemplate for typeof GroupToExport. Create a partial view /Views/Shared/EditorTemplates/GroupToExport.cshtml

@model yourAssembly.GroupToExport
<tr>
  <td class="js-export-checkbox">
    @Html.CheckBoxFor(m => m.ToExport)
  </td>
</tr>

然后在主视图中

@Html.EditorFor(m => m.ExportingGroups)

EditorFor() 方法将根据模板为集合中的每个项目生成正确的 html.

The EditorFor() method will generate the correct html for each item in your collection based on the template.

这篇关于剃刀中的多个复选框(使用 foreach)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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