如何通过IEnumerable的名单MVC到控制器,包括复选框状态? [英] How to pass IEnumerable list to controller in MVC including checkbox state?

查看:151
本文介绍了如何通过IEnumerable的名单MVC到控制器,包括复选框状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MVC应用程序中,我使用这样的模式:

I have an mvc application in which I am using a model like this:

 public class BlockedIPViewModel
{
    public string  IP { get; set; }
    public int ID { get; set; }
    public bool Checked { get; set; }
}

现在我有一个观点来绑定像这样的列表:

Now I have a View to bind a list like this:

@model IEnumerable<OnlineLotto.Web.Models.BlockedIPViewModel>
@using (Html.BeginForm())
{
  @Html.AntiForgeryToken()
}

@foreach (var item in Model) {
<tr>
    <td>

        @Html.HiddenFor(x => item.IP)           
        @Html.CheckBoxFor(x => item.Checked)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.IP)
    </td>

</tr>
}

<div>
    <input type="submit" value="Unblock IPs" />
</div>

现在我有一个控制器接收操作从提交按钮:

Now I have a controller to receive action from submit button:

 public ActionResult BlockedIPList(IEnumerable<BlockedIPViewModel> lstBlockedIPs)
 {

  }

不过来控制器action.I必要在这里获得的复选框状态时,我得到空值lstBlockedIPs。请帮助。

But I am getting null value to lstBlockedIPs when coming to controller action.I need to get the checkbox state here. Please help.

推荐答案

使用一个列表,而不是更换你的的foreach 循环中的循环:

Use a list instead and replace your foreach loop with a for loop:

@model IList<BlockedIPViewModel>

@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken()

    @for (var i = 0; i < Model.Count; i++) 
    {
        <tr>
            <td>
                @Html.HiddenFor(x => x[i].IP)           
                @Html.CheckBoxFor(x => x[i].Checked)
            </td>
            <td>
                @Html.DisplayFor(x => x[i].IP)
            </td>
        </tr>
    }
    <div>
        <input type="submit" value="Unblock IPs" />
    </div>
}

另外,您可以使用编辑器的模板:

Alternatively you could use an editor template:

@model IEnumerable<BlockedIPViewModel>

@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken()
    @Html.EditorForModel()   
    <div>
        <input type="submit" value="Unblock IPs" />
    </div>
}

,然后定义模板〜/查看/共享/ EditorTemplates / BlockedIPViewModel.cshtml 将自动被渲染为集合中的每个元素:

and then define the template ~/Views/Shared/EditorTemplates/BlockedIPViewModel.cshtml which will automatically be rendered for each element of the collection:

@model BlockedIPViewModel
<tr>
    <td>
        @Html.HiddenFor(x => x.IP)
        @Html.CheckBoxFor(x => x.Checked)
    </td>
    <td>
        @Html.DisplayFor(x => x.IP)
    </td>
</tr>

你让你的控制器空的原因是因为你不尊重你的输入域的命名惯例,默认的模型绑定期望成功绑定到一个列表。我请你阅读下面的文章< / code> 。

一旦你读它,看一下生成的HTML(和输入字段的更具体的名称)以我的例子和你的。然后比较,你就会明白,为什么你不工作。

Once you have read it, look at the generated HTML (and more specifically the names of the input fields) with my example and yours. Then compare and you will understand why yours doesn't work.

这篇关于如何通过IEnumerable的名单MVC到控制器,包括复选框状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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