本文介绍了如何将 IEnumerable 列表传递给 MVC 中的控制器,包括复选框状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 mvc 应用程序,我在其中使用了这样的模型:

 公共类 BlockedIPViewModel{公共字符串 IP { 获取;放;}公共 int ID { 获取;放;}公共布尔检查{得到;放;}}

现在我有一个视图来绑定这样的列表:

@model IEnumerable@using (Html.BeginForm()){@Html.AntiForgeryToken()}@foreach(模型中的变量项目){<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"/>

现在我有一个控制器来接收来自提交按钮的动作:

 public ActionResult BlockedIPList(IEnumerable lstBlockedIPs){}

但是我在执行控制器操作时得到了 lstBlockedIPs 的空值.我需要在此处获取复选框状态.请帮忙.

解决方案

改用列表并将 foreach 循环替换为 for 循环:

@model IList@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"/>

}

或者,您可以使用编辑器模板:

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

}

然后定义模板 ~/Views/Shared/EditorTemplates/BlockedIPViewModel.cshtml 它将为集合的每个元素自动呈现:

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

您在控制器中获取 null 的原因是因为您没有遵守默认模型绑定器希望成功绑定到列表的输入字段的命名约定.我邀请您阅读以下文章.

阅读后,用我和你的例子查看生成的 HTML(更具体地说是输入字段的名称).然后比较,你就会明白为什么你的不起作用.

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)
 {

  }

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

解决方案

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>
}

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>

The reason you were getting null in your controller is because you didn't respect the naming convention for your input fields that the default model binder expects to successfully bind to a list. I invite you to read the following article.

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 中的控制器,包括复选框状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 10:05