【问题标题】:How do you move items betwen two listboxes on MVC - JQuery not working如何在 MVC 上的两个列表框之间移动项目 - JQuery 不起作用
【发布时间】:2014-10-20 13:53:51
【问题描述】:

我已经查看了关于此的其他 SO 主题,但它们最终要么非常老旧,要么使用 WebForms。我有一个 MVC 视图,其中有两个列表框。我想在两个列表框之间来回移动项目。视图是:

  @using (Html.BeginForm())
    {
       @Html.ListBoxFor(m => m.SelectedAttributes, Model.Attributes, new {id="listBoxAvail", SIZE = 5} ) 

        <input type="submit" name="add" 
               id="add" value="MoveRight" />

        <input type="submit" name="remove" 
               id="remove" value="MoveLeft" />

        @Html.ListBoxFor(m => m.SelectedAttributes2, Model.SelectedItems, new { id = "listBoxSel", SIZE = 5})
    } 

ViewModel 是:

    public class OptInViewModel
        {
            public IEnumerable<string> SelectedAttributes { get; set; }
            public IEnumerable<string> SelectedAttributes2 { get; set; }
            public IEnumerable<SelectListItem> Attributes { get; set; }
            public IEnumerable<SelectListItem> SelectedItems { get; set; }
        }

And the Controller code is:

 public ActionResult Index()
        {
            AttributeEntities db = new AttributeEntities();
            List<SelectListItem> listSelectListItems = new List<SelectListItem>();
            List<SelectListItem> listSelItems = new List<SelectListItem>();

            foreach (var attributes in db.HarmonyAttributes)
            {
                SelectListItem selectList = new SelectListItem
                {
                    Text = attributes.AttributeName,
                    Value = attributes.AtrributeLabel,
                    Selected = false
                };
                listSelectListItems.Add(selectList);
            }

            foreach (var sel in db.SelectedHarmonyAttributes)
            {
                SelectListItem selList = new SelectListItem
                {
                    Text = sel.CustomLabel,
                    Value = sel.HarmonyAttribute_ID.ToString(),
                    Selected = false
                };
                listSelectListItems.Add(selList);
            }

            OptInViewModel viewModel = new OptInViewModel
            {
                Attributes = listSelectListItems,
                SelectedItems = listSelItems
            };


            return View(viewModel);
        }

我使用 JQuery 来尝试执行此操作,但它不起作用(没有任何内容传输到第二个列表框)。有人知道怎么了?

<script src="~/Scripts/jquery-2.1.1.js"></script>
    <script>
        $(function () {
            $("add").click(function () {
                $("#listBoxAvail > option:selected").each(function () {
                    $(this).remove().appendTo("#listBoxSel");
                });
            });

            $("remove").click(function () {
                $("#listBoxSel > option:selected").each(function () {
                    $(this).remove().appendTo("#listBoxAvail");
                });
            });
        });
</script>

【问题讨论】:

  • 是的,除非您进行回发或 ajax 更新,否则您需要使用 javascript
  • 你能给我看看 JavaScript 代码吗?
  • 我从那里得到了一些 JQuery。修改它,但我无法让它工作

标签: javascript c# jquery asp.net-mvc


【解决方案1】:

将按钮类型从提交到按钮替换为如下所示

<input type="button" name="add" id="add" value="MoveRight" />
<input type="button" name="remove" id="remove" value="MoveLeft" />

在您的 JavaScript 中,正确的选择器在 ID 前面加上 #,它应该可以工作!

$("add")$("#add")

$("remove")$("#remove")

如果你愿意,你可以把它减少到

  <script>
    $(function() {
      $(document)
        .on("click", "#add", function() {
          $("#listBoxAvail :selected").remove().appendTo("#listBoxSel");
        })
        .on("click","#remove", function() {
          $("#listBoxSel :selected").remove().appendTo("#listBoxAvail");
        });
    });
  </script>

【讨论】:

    【解决方案2】:

    您可以使用 jQuery 的 jQuery-Dual-Listbox 插件。它需要一些设置,但在 MVC 环境中运行良好。需要注意的是,为了将您在第二个列表框中选择的值发送回服务器进行处理,您需要确保该列表框中的所有项目都是selected,然后才能提交表单。

    例如为您的标记:

    <script src="jQuery.dualListBox-1.3.js" type="text/javascript"/>
    <script type="text/javascript">
    $(function () {
        $.configureBoxes({
            box1View: 'listBoxAvail',
            box2View: 'listBoxSel',
            to1: 'remove',
            to2: 'add',
            allTo1: 'remove-all',  //you must create this button
            allTo2: 'add-all',     //you must create this button
            useFilters: false
        });
    });
    $("#listBoxSel").closest("form").on("submit",
        function() {
            //only selected options in listbox get POSTed back!
            $("#listBoxSel > option").prop("selected", true);
            return true;
        }
    }
    </script>
    

    Source

    【讨论】:

    • 我试过了,但它在 $("#SelectedAttributes2 > option").prop("selected", true); 行中给了我一个错误;
    • 我已经编辑了代码示例以匹配您的控件名称 (#listBoxSel),但即使这样也不应该导致错误。它根本不会做任何事情。你得到什么错误?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多