MVC2 - 如何获取复选框数据?
好的,现在我有一个包含此选择列表的表单:
<%=Html.LabelFor(x => x.Id)%>
<%= Html.ListBoxFor(x => x.Ids, new SelectList(Model.Items, "ID", "Name", Model.Ids)) %><br />
这是我的控制器,我将数据添加到数据库中:
foreach (var id in model.Ids)
{
tool.ToolItems.Add(new ToolItem { ID = id });
}
现在,我决定使用复选框而不是选择列表,这是代码:
<% foreach (var item in Model.Tools)
{ %>
<input type="checkbox" name="tool" value="<%= item.ID %>" />
<%= tool.Name %>
</>
<% } %><br />
我不知道在我的控制器中该怎么做才能获取所有选定的复选框。
抱歉,如果某些变量没有意义,我尝试尽可能一致地更改它,因为我无法发布实际的内容。
Okay, right now I have a form that contains this select list:
<%=Html.LabelFor(x => x.Id)%>
<%= Html.ListBoxFor(x => x.Ids, new SelectList(Model.Items, "ID", "Name", Model.Ids)) %><br />
And here is my controller, where I add the data to the database:
foreach (var id in model.Ids)
{
tool.ToolItems.Add(new ToolItem { ID = id });
}
Now, I decided to use checkboxes instead of a selectlist, here is that code:
<% foreach (var item in Model.Tools)
{ %>
<input type="checkbox" name="tool" value="<%= item.ID %>" />
<%= tool.Name %>
</>
<% } %><br />
I have no idea what to do in my controller tho to get all the selected checkboxes.
Sorry if some of the variables dont make sense, I tried to change it as consistently as possible since I cant post the actual stuff.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能会发现以下问题很有帮助: 将复选框选择传递给 要回答您的问题,您的控制器操作
可能需要如下所示:
You might find the following question helpful: Passing checkbox selection through to an action
To answer your question, your controller action probably needs to look like this: