【问题标题】:Jquery multiselect comes back with one selected after POSTJquery multiselect 在 POST 之后返回一个选择
【发布时间】:2014-10-16 09:39:53
【问题描述】:

我有一个Jquery multiselect 控件

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

这是链接到DropDownListFor

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

当我按下按钮发帖时,强类型模型会发送列表中被选中的任何人。好的。但是,发布后,它只显示DropDownListFor 中的第一个选定项目,而不显示其他项目。当帖子发生时,我会用所选的 ID 将模型发回

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

我是否需要对Jqueryrazor 侧做一些事情以在回发时显示其他选定的项目?

谢谢

【问题讨论】:

  • 不确定这是否是解析错误,但您的代码中的括号肯定有问题。还有一个额外的});
  • 对不起,我没有复制整个 JS,但我发布的内容你是对的!
  • 返回视图时应该可以正常工作。您是否尝试过不使用jquery multiselect - 只使用@Html.DropDownListFor 生成的默认html?
  • @StephenMuecke 不,但我现在只有一个选择,所以它不是jquery,这让我有更多的时间去调查,谢谢
  • 虽然我使用 @Html.ListBoxFor(..new SelectList(.. 对我有用,但我认为它们生成相同的 html,所以应该没有任何区别

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


【解决方案1】:

msdn 中提供一个接收选定值的构造函数。

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

【讨论】:

  • 啊,我明白了,但是我的模型没有 ToList() 我想我需要 x.ID 包含的集合?
  • 您应该使用选定的 ID 传递 IEnumerable。很简单。
【解决方案2】:

哇,好的,我发现了问题。

我不确定它是 .NET 错误还是我需要在代码中更改的内容,但正在更改

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

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

神奇地起作用。

现在,在我寻找解决方案的过程中,我从来没有看到过一篇帖子说 “DropDownListFor 不能与 multiSelectList 一起使用 或其中的任何内容,但我很乐意有人解释如果是不是我做错了什么?

但是,这可能会节省人们花费数小时寻找解决方案的时间。

感谢所有帮助过的人:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-08
    相关资源
    最近更新 更多