在MVC中将复选框绑定到int数组/枚举 [英] Bind Checkboxes to int array/enumerable in MVC
问题描述
@Html.CheckBox("orderNumbers", new { value = 1 })
@Html.CheckBox("orderNumbers", new { value = 2 })
@Html.CheckBox("orderNumbers", new { value = 3 })
@Html.CheckBox("orderNumbers", new { value = 4 })
@Html.CheckBox("orderNumbers", new { value = 5 })
[HttpPost]
public ActionResult MarkAsCompleted(IEnumerable<int> orderNumbers) { }
[HttpPost]
public ActionResult MarkAsCompleted(IEnumerable<string> orderNumbers) { }
如果我在操作中使用第一个签名方法,我得到一个空的 IEnumerable
。
If I use the first signature in my action method, I get an empty IEnumerable
.
如果我使用第二个签名我收到的值,为未选择的值接收一个假值(因为使用隐藏字段来隐藏所有复选框的MVC模式)。
If I use the second signature I do receive the values but I also receive a false value for the unselected values (because of MVCs pattern of shadowing all checkboxes with a hidden field).
例如我会收到 orderNumbers = {1,2,false,4,false}
为什么我不能得到数字列表?
Why can't I just get the list of numbers?
推荐答案
c $ c> CheckBoxFor 帮助器正在工作。
Because thats how the provided CheckBoxFor
helper is working.
您必须自己为复选框生成html。然后不生成隐藏的输入,您将只获得所选的整数值。
You have to generate the html for the checkboxes yourself. Then the hidden inputs are not generated and you will get only the selected integer values.
这篇关于在MVC中将复选框绑定到int数组/枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!