formcollection 仅保存选定的 html.listbox 项目值?多维控制器

发布于 2024-08-24 02:27:55 字数 663 浏览 4 评论 0原文

我的情况是这样的:我有两个列表框,一个包含所有数据库项目,另一个是空的。用户将完整列表框中所需的项目添加到空列表框中。

我正在使用表单提交用户添加的所有项目。

问题是,仅提交列表框中选定的项目。因此,如果用户取消选择某些项目,它们将不会在表单中提交。我的观点看起来像这样:

<% using (Html.BeginForm("MyAction", "MyController"))
   { %>

    <%= Html.ListBox("AddedItems", Model.Items)%>

    <input type="submit" value="Submit" name="SubmitButton"/>
<% } %>

我的控制器看起来像这样:

public ActionResult MyAction(FormCollection formCollection)
{
    var addedItems = formCollection["AddedItems"].Split(',');

    //....more code that does stuff with the items
}

我是否以错误的方式处理所有事情?有没有更好的方式来提交项目?你会怎么办?

My scenario is this: I have two listbox's, one that contains all my database items, and an empty one. The user adds the items needed from the full listbox to the empty listbox.

I'm using a form to submit all the items the user has added.

The problem is, only the selected items from the listbox are submitted. So if the user deselects some of the items, they wont be submitted in the form. My view looks like so:

<% using (Html.BeginForm("MyAction", "MyController"))
   { %>

    <%= Html.ListBox("AddedItems", Model.Items)%>

    <input type="submit" value="Submit" name="SubmitButton"/>
<% } %>

My Controller looks like so:

public ActionResult MyAction(FormCollection formCollection)
{
    var addedItems = formCollection["AddedItems"].Split(',');

    //....more code that does stuff with the items
}

Am I going about everything the wrong way? Is there better way to submit the items? What would you do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

就是爱搞怪 2024-08-31 02:27:55

我也在这样做,我认为我解决它的方式更优雅一点。本质上,我只是有一个 Jquery 函数,该函数在选择所有选项的表单发布之前运行。

    $(function () {
        $("form").submit(function (e) {
            $("#box2View option").attr("selected", "selected");
        });
    });

I am doing this as well, I think the way I solved it is a bit more elegant. Essentially I just have a Jquery function that runs before the form post that selects all the options.

    $(function () {
        $("form").submit(function (e) {
            $("#box2View option").attr("selected", "selected");
        });
    });
居里长安 2024-08-31 02:27:55

因为它只是选择框。您无法在选择框中发布所有值。您必须使用 javascript 来捕获添加的项目并将它们存储在隐藏输入中。

未经测试的代码,但我认为它对你有帮助。

<script type="text/javascript">
    function addItem() {
        var allItems = document.getElementById("AllItems");
        var op = allItems.options[allItems.selectedIndex];
        var hdSelectedItems = document.getElementById("hdSelectedItems");
        var lbSelectedItems = document.getElementById("lbSelectedItems");

        lbSelectedItems.options[lbSelectedItems.options.length] = op;

        if (hdSelectedItems.value != '') {
             hdSelectedItems.value += ","
        }
        hdSelectedItems.value += op.value;
    }
</script>
<%= Html.Hidden("hdSelectedItems") %>
<%= Html.ListBox("AllItems", Model.Items)%>
<%= Html.ListBox("lbSelectedItems") %>
<a href="#" onclick="addItem(); return false;">Add</a>

Because it's just selectbox. You cannot post all values in selectbox. You have to use javascript to catch added items and store them in hidden input.

Un-tested code, but i think it help you.

<script type="text/javascript">
    function addItem() {
        var allItems = document.getElementById("AllItems");
        var op = allItems.options[allItems.selectedIndex];
        var hdSelectedItems = document.getElementById("hdSelectedItems");
        var lbSelectedItems = document.getElementById("lbSelectedItems");

        lbSelectedItems.options[lbSelectedItems.options.length] = op;

        if (hdSelectedItems.value != '') {
             hdSelectedItems.value += ","
        }
        hdSelectedItems.value += op.value;
    }
</script>
<%= Html.Hidden("hdSelectedItems") %>
<%= Html.ListBox("AllItems", Model.Items)%>
<%= Html.ListBox("lbSelectedItems") %>
<a href="#" onclick="addItem(); return false;">Add</a>
雨后彩虹 2024-08-31 02:27:55

为什么不在复选框中包含项目列表。
然后,您可以遍历操作中的复选框并获取所有选定的复选框。

<% foreach(var item in Model.Items) { %>

   <%= Html.CheckBox(String.Format("ItemID.{0}", item.ID)) %> // each item tagged by the items id

<% } %>

public ActionResult MyAction(FormCollection formCollection)
{

            foreach (var key in collection.AllKeys.Where(k => !k.Contains("SubmitButton")).ToArray<string>())
            {
                 // iterates thru check boxes we got rid of the button 

            }
}

Why not have the list of items in checkboxes.
Then you could iterate through the checkboxes in your action and grab all selected checkboxes.

<% foreach(var item in Model.Items) { %>

   <%= Html.CheckBox(String.Format("ItemID.{0}", item.ID)) %> // each item tagged by the items id

<% } %>

public ActionResult MyAction(FormCollection formCollection)
{

            foreach (var key in collection.AllKeys.Where(k => !k.Contains("SubmitButton")).ToArray<string>())
            {
                 // iterates thru check boxes we got rid of the button 

            }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文