formcollection 只保存选定的 html.listbox 项目值?MVC [英] formcollection only holds the selected html.listbox items values? MVC

查看:20
本文介绍了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天全站免登陆