MVC2 - 如何获取复选框数据?

发布于 2024-12-22 13:00:50 字数 745 浏览 5 评论 0原文

好的,现在我有一个包含此选择列表的表单:

<%=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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

五里雾 2024-12-29 13:00:50

您可能会发现以下问题很有帮助: 将复选框选择传递给 要回答您的问题,您的控制器操作

可能需要如下所示:

public ActionResult MyAction(string[] tool) {
    // "tool" will contain the values of all checked boxes!
}

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:

public ActionResult MyAction(string[] tool) {
    // "tool" will contain the values of all checked boxes!
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文