如何正确创建 MultiSelect
(抱歉,这里有几个项目,但似乎没有一个项目允许我完成这项工作。)
我想创建一个允许多项选择的 DropDownList。我可以填充列表,但无法使当前选定的值看起来起作用。
我的控制器中有以下内容:
ViewBag.PropertyGroups = from g in db.eFinGroups
where g.GroupType.Contents == "P"
select new
{
Key = g.Key,
Value = g.Description,
Selected = true
};
ViewBag.SelectedPropertyGroups = 来自company.Entities中的g
.First().Properties.First().PropertyGroups
选择新的{
g.eFinGroup.Key,
值 = g.eFinGroup.Description };
在我看来:
@Html.DropDownListFor(model => model.PropertyGroupsX,
new MultiSelectList(ViewBag.PropertyGroups
, "Key", "Value"
, ViewBag.SelectedPropertyGroups),
new { @class = "chzn-select", data_placeholder = "Choose a Property Group", multiple = "multiple", style = "width:350px;" })
PropertyGroupX 是模型中的 string[]。
我已经尝试了对所选属性的所有类型的迭代...仅传递值、仅传递键、两者等等。
另外,PropertyGroupX 应该是什么类型?字符串数组是否正确?或者它应该是包含当前属性组的字典?我真的很难找到这方面的文档。
有人建议我应该使用 ListBoxFor。我已经改变了,但仍然有同样的问题。呈现选项标签时,所选值不会设置为所选值。这是我尝试过的:
@Html.ListBoxFor(model => model.PropertyGroups, new MultiSelectList(ViewBag.PropertyGroups, "Key", "Value"))
我尝试将 model.PropertyGroups 作为匹配的字符串集合值,作为与此 ID 匹配的 Guid 集合,并作为具有键和值的匿名类型来匹配 ViewBag 中的项目。似乎没什么作用。
(sorry, there are several item here but none seems to allow me to get this working.)
I want to create a DropDownList which allows multiple selection. I am able to populate the list but I can't get the currently selected values to seem to work.
I have the following in my controller:
ViewBag.PropertyGroups = from g in db.eFinGroups
where g.GroupType.Contents == "P"
select new
{
Key = g.Key,
Value = g.Description,
Selected = true
};
ViewBag.SelectedPropertyGroups = from g in company.Entities .First().Properties.First().PropertyGroups select new { g.eFinGroup.Key, Value = g.eFinGroup.Description };
In the view I have:
@Html.DropDownListFor(model => model.PropertyGroupsX,
new MultiSelectList(ViewBag.PropertyGroups
, "Key", "Value"
, ViewBag.SelectedPropertyGroups),
new { @class = "chzn-select", data_placeholder = "Choose a Property Group", multiple = "multiple", style = "width:350px;" })
PropertyGroupX is a string[] in the model.
I have tried all types of iterations with the selected properties... passing just the value, just the key, both, etc.
Also, what type is PropertyGroupX supposed to be? Is string array correct? Or should it be a dictionary that contains the current propertygroups? I really am having a hard time finding doc on this.
Someone suggested I should be using ListBoxFor. I have changed to that and still have the same issue. The selected values are not being set as selected when the option tags are rendered. Here is what I have tried:
@Html.ListBoxFor(model => model.PropertyGroups, new MultiSelectList(ViewBag.PropertyGroups, "Key", "Value"))
I have tried the model.PropertyGroups as a collection of string matching the Values, as a collection of Guid matching this IDs and as an anonymous type with both a Key and Value to match the items in the ViewBag. Nothing seems to work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想创建多选列表,则不要使用
DropDownListFor
。您使用ListBoxFor
帮助器。视图模型:
控制器:
视图:
You don't use
DropDownListFor
if you want to create a multiselect list. You use theListBoxFor
helper.View model:
Controller:
View: