的FormCollection仅持有选定html.listbox物品价值? MVC [英] formcollection only holds the selected html.listbox items values? MVC

查看:113
本文介绍了的FormCollection仅持有选定html.listbox物品价值? MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的情况是这样的:我有两个列表框的,一个包含我所有的数据库项目,并创建一个空。用户添加从全列表框为空列表框所需要的物品。

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?

推荐答案

我这样做,以及,我想我解决它的办法是更优雅一点。基本上我只是有一个选择的所有选项的表单提交之前运行jQuery的功能。

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");
        });
    });

这篇关于的FormCollection仅持有选定html.listbox物品价值? MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆