简体   繁体   English

jQuery multiselect在POST之后返回一个选择

[英]Jquery multiselect comes back with one selected after POST

I have a Jquery multiselect control 我有一个Jquery multiselect控件

$(function () {
    $(".multiselect").multiselect().multiselectfilter({
        filter: function (event, matches) {
            if (matches.length != null && !matches.length) {
                // do something
            }
        }
    });
});

And this is linked to a DropDownListFor 并将其链接到DropDownListFor

@Html.DropDownListFor(x => x.ID, new MultiSelectList(Model.Employees, "Id", "Name"), "Please Select", new {multiple = "multiple", @class = "multiselect"})  

When I press a button to post, the strongly typed model does send whoever has been selected in the list. 当我按下按钮发布时,强类型模型会发送列表中选择的任何人。 Good. 好。 However, after the post, it only displays the first selected item in the DropDownListFor and not the others. 但是,发布后,它仅显示DropDownListFor的第一个选定项目,而不显示其他项目。 I do send the model back when the post has happened with the IDs that are selected 当发布带有所选ID的信息时,我确实将模型发回

// Model.ID which is a `int[]` is bounded to the `DropDownListFor` above has multiple values inside it. 
return View("View" Model)

Do I need to do something to the Jquery or the razor side to display the other selected items on postback? 我是否需要对Jqueryrazor侧做一些操作以在回发中显示其他选定项?

Thanks 谢谢

In msdn present a constructor which receives selectedValues. msdn中,提供一个接收selectedValues的构造函数。

public MultiSelectList(
    IEnumerable items,
    string dataValueField,
    string dataTextField,
    IEnumerable selectedValues
)

@Html.DropDownListFor(x => x.ID, new MultiSelectList(Model.Employees, "Id", "Name", Model.ToList()), "Please Select", new {multiple = "multiple", @class = "multiselect"}) 

Wow, OK, I found the problem. 哇,好,我发现了问题。

I am not sure if its a .NET bug or something I need to change in my code, but changing 我不确定它是否是.NET错误或我需要在代码中进行更改的内容,但是

@Html.DropDownListFor(x => x.ID, new MultiSelectList(Model.Employees, "Id", "Name"), "Please Select", new {multiple = "multiple", @class = "multiselect"}) 

To

@Html.ListBoxFor(x => x.ID, new MultiSelectList(Model.Employees, "Id", "Name"), "Please Select", new {multiple = "multiple", @class = "multiselect"}) 

Just magically works. 只是神奇地工作。

Now, In my searching for a solution, I never saw one post to say "DropDownListFor's do not work with multiSelectList or anything on them lines but I wou;ld be happy for someone to explain if its something I done wrong? 现在,在寻找解决方案的过程中,我再也没有看到一个帖子说“ DropDownListFor不能与multiSelectList或它们上的任何行一起工作,但是我很乐意有人解释它做错了什么吗?

However, this may save someone searching for hours for the solution. 但是,这样可以节省一些人寻找解决方案的时间。

Thanks to everyone who helped :) 感谢所有帮助的人:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将多选选定选项作为URL中的一个参数发布-Bootstrap Multiselect - How to post multiselect selected options as one param in url - Bootstrap Multiselect JQuery removeClass之后类又回来了 - Class comes back after JQuery removeClass 在Jquery UI Multiselect小部件中回发后,所选项目仍保持选中状态 - Selected items remains selected after postback in Jquery UI Multiselect widget 在JQuery Ajax中发布多选列表的所有选定项目 - Post all selected items of multiselect list in JQuery Ajax 在使用jQuery单击后退按钮后,如何检查页面是否出现? - How to check if a page comes after clicking the back button using jQuery? jQuery div从左到右动画,并在暂停后返回 - jQuery div animate from left to right and comes back after pause 回发后如何保持状态以突出显示JQuery Treeview的选定节点? - How to maintain state to highlighting selected node of a JQuery Treeview after Post Back? 当用户返回页面时,如何使用jquery保持所选菜单处于活动状态? - How to keep the selected menu active using jquery, when the user comes back to page? jQuery 在发布数据后添加选择到选项 - jQuery add selected to option after post data 多选,jQuery:检查我的多选,是否至少应选择一个选项 - Multiselect, Jquery: Check my multiple select if at least one options should be selected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM