在ASP.NET MVC中使用HTML radiobutton数组

时间:2022-12-22 11:27:38

I tried to follow this but the default modelbinder let my array null on the server side.

我试图遵循这个,但默认的模型绑定器让我的数组在服务器端null。

HTML:

Question 1:
<input name="list[0]" type="radio" value="1000" />No
<input name="list[0]" type="radio" value="1001" />Yes
Question 2:
<input name="list[1]" type="radio" value="1002" />No
...

Controller action:

 public ActionResult Anamnesis(string[] list)
 {

If I choose only the second "No" (list[0] is missing) then the DefaultModelBinder is impossible to transform it into an array.

如果我只选择第二个“No”(缺少list [0]),则DefaultModelBinder无法将其转换为数组。

Thanx in advance!

Thanx提前!

Update#1

Reformatted based on the comment, thank you!

根据评论重新格式化,谢谢!

Update#2

Just a tought: created a hidden input after all list item, and in this way it works. But it's ugly, no doubt.

只是一个tought:在所有列表项之后创建一个隐藏的输入,并以这种方式工作。但毫无疑问,这很丑陋。

Question 1:
<input name="list[0]" type="radio" value="1000" />No
<input name="list[0]" type="radio" value="1001" />Yes   
<input type="hidden" name="list[0]"/>
Question 2:
<input name="list[1]" type="radio" value="1002" />No
<input type="hidden" name="list[1]"/>
...

Order it's very important: the hidden value submits only when the radio is unchecked. The idea it's from the ASP.NET MVC helpers. (Btw I cannot use Html.RadioButton to archive this behavior.)

命令它非常重要:只有在取消选中无线电时才会提交隐藏值。这个想法来自ASP.NET MVC助手。 (顺便说一句,我不能使用Html.RadioButton来存档此行为。)

2 个解决方案

#1


Your Update #2 seems like it solves your problem. Your Update #2 is also interesting in that you could also use this approach to supply a default value (such as 999) to be used whenever nothing is checked.

您的更新#2似乎解决了您的问题。您的更新#2也很有趣,您也可以使用此方法提供默认值(例如999),以便在检查任何内容时使用。

There is perhaps another similar way to do what you are asking, which is based on this article and which also uses hidden inputs. The idea is that you can create indexes for each of your radio sets, to avoid the situation where a missing selection earlier in the form causes all subsequent selections to be dropped:

也许还有另一种类似的方法来做你要求的,这是基于这篇文章,也使用隐藏的输入。我们的想法是,您可以为每个无线电集创建索引,以避免表单中较早的选择丢失导致所有后续选择被删除的情况:

Question 1:
<input name="list.Index" type="hidden" value="0" />
<input name="list[0]" type="radio" value="1000" />No
<input name="list[0]" type="radio" value="1001" />Yes
Question 2:
<input name="list.Index" type="hidden" value="1" />
<input name="list[1]" type="radio" value="1000" />No
<input name="list[1]" type="radio" value="1001" />Yes

The reason I suggest this, is in the case where you might like to associate your answers with a specific question by a unique ID, instead of just using 0, 1, 2 etc. The article I linked will show an example of how to do this.

我建议这样做的原因是,您可能希望通过唯一ID将您的答案与特定问题相关联,而不是仅使用0,1,2等。我链接的文章将显示如何操作的示例这个。

Good luck!
-Mike

祝好运! -麦克风

#2


The name attribute of the radio button should be list, not list[n].

单选按钮的name属性应该是list,而不是list [n]。

#1


Your Update #2 seems like it solves your problem. Your Update #2 is also interesting in that you could also use this approach to supply a default value (such as 999) to be used whenever nothing is checked.

您的更新#2似乎解决了您的问题。您的更新#2也很有趣,您也可以使用此方法提供默认值(例如999),以便在检查任何内容时使用。

There is perhaps another similar way to do what you are asking, which is based on this article and which also uses hidden inputs. The idea is that you can create indexes for each of your radio sets, to avoid the situation where a missing selection earlier in the form causes all subsequent selections to be dropped:

也许还有另一种类似的方法来做你要求的,这是基于这篇文章,也使用隐藏的输入。我们的想法是,您可以为每个无线电集创建索引,以避免表单中较早的选择丢失导致所有后续选择被删除的情况:

Question 1:
<input name="list.Index" type="hidden" value="0" />
<input name="list[0]" type="radio" value="1000" />No
<input name="list[0]" type="radio" value="1001" />Yes
Question 2:
<input name="list.Index" type="hidden" value="1" />
<input name="list[1]" type="radio" value="1000" />No
<input name="list[1]" type="radio" value="1001" />Yes

The reason I suggest this, is in the case where you might like to associate your answers with a specific question by a unique ID, instead of just using 0, 1, 2 etc. The article I linked will show an example of how to do this.

我建议这样做的原因是,您可能希望通过唯一ID将您的答案与特定问题相关联,而不是仅使用0,1,2等。我链接的文章将显示如何操作的示例这个。

Good luck!
-Mike

祝好运! -麦克风

#2


The name attribute of the radio button should be list, not list[n].

单选按钮的name属性应该是list,而不是list [n]。