asp.net-mvc MVC 绑定到复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9580986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:47:08  来源:igfitidea点击:

MVC Binding to checkbox

asp.net-mvc

提问by TBD

I have found so many questions about this, but none of them go over or seem to go over my scenario. I have a model:

我发现了很多关于此的问题,但没有一个超过或似乎超过我的场景。我有一个模型:

public class CheckBoxModel
{
            public int Id{ get; set; }
    public bool IsSelected { get; set;  }
}

In then try and bind my IsSelected bool to a checkbox like this:

然后尝试将我的 IsSelected bool 绑定到这样的复选框:

<%= Html.CheckBox("IsSelectedCheck",Model.IsSelected)%>

I have lots of items on a page, they all have a checkbox next to them, all i am trying to do is pass back to the controller all the id's of the items and which ones have been selected.

我在一个页面上有很多项目,它们旁边都有一个复选框,我要做的就是将所有项目的 id 和已选择的项目的 ID 传回控制器。

At the moment, the value of IsSelected is always false. Should Html.CheckBox set the value of Model.IsSelected each time the user toggles the checkbox.

目前,IsSelected 的值始终为 false。每次用户切换复选框时,Html.CheckBox 是否应该设置 Model.IsSelected 的值。

Thanks

谢谢

回答by Darin Dimitrov

Try like this:

像这样尝试:

<%= Html.CheckBoxFor(x => x.IsSelected) %>

Also if you want to pass along the id don't forget to do so:

另外,如果您想传递 id,请不要忘记这样做:

<%= Html.HiddenFor(x => x.Id) %>

And if you had a collection of those:

如果你有这些集合:

public class MyViewModel
{
    public CheckBoxModel[] CheckBoxes { get; set; }
}

you could:

你可以:

<% for (var i = 0; i < Model.CheckBoxes.Length; i++) { %>
    <div>
        <%= Html.HiddenFor(x => x.CheckBoxes[i].Id) %>
        <%= Html.CheckBoxFor(x => x.CheckBoxes[i].IsSelected) %>
    </div>
<% } %>

which will successfully bind to:

这将成功绑定到:

[HttpPost]
public ActionResult MyAction(MyViewModel model) 
{
    // model.CheckBoxes will contain everything you need here
    ...
}

回答by Scotty.NET

An alternative to Darin's fantastic answer

达林绝妙答案的替代方案

I definitely recommend following Darin's approach for returning classes which will be most of the time. This alternative is a 'quick' and dirty hack if all you need is the checked Ids:

我绝对建议遵循 Darin 的方法来返回类,这将是大部分时间。如果您只需要检查 Id,则此替代方案是一种“快速”和肮脏的 hack:

<% foreach (var cb in Model.CheckBoxes) { %>
  <div>
    <input type="checkbox" 
      value="<%= cb.Id %>"
      <%= cb.IsSelected ? "checked=\"checked\"" : "" %>
      name="ids" />
  </div>
<% } %>

Will bind to the int[] idsparameter in the following action:

将绑定到int[] ids以下操作中的参数:

[HttpPost]
public ActionResult MyAction(int[] ids) 
{
    // ids contains only those ids that were selected
    ...
}
  • The benefit is cleaner html as there is no hidden input.
  • The cost is writing more code in the view.
  • 好处是 html 更干净,因为没有隐藏的输入。
  • 代价是在视图中编写更多代码。

In MVC 4.0 (Razor 2.0) you can use the following syntax in your view:

在 MVC 4.0 (Razor 2.0) 中,您可以在视图中使用以下语法:

<input type="checkbox" value="@cb.Id" checked="@cb.IsSelected" name="ids" />