你如何正确地创建一个 MultiSelect <select>使用 DropdownList 助手? [英] How do you properly create a MultiSelect <select> using the DropdownList helper?

查看:20
本文介绍了你如何正确地创建一个 MultiSelect <select>使用 DropdownList 助手?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(抱歉,这里有几个项目,但似乎没有一个可以让我完成这项工作.)

(sorry, there are several item here but none seems to allow me to get this working.)

我想创建一个允许多选的 DropDownList.我可以填充列表,但我无法让当前选择的值看起来有效.

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选择新的{g.eFinGroup.Key,值 = g.eFinGroup.Description };

ViewBag.SelectedPropertyGroups = from g in company.Entities .First().Properties.First().PropertyGroups select new { g.eFinGroup.Key, Value = 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 是模型中的字符串[].

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.

另外,PropertyGroupX 应该是什么类型?字符串数组是否正确?或者它应该是一个包含当前属性组的字典?我真的很难找到这方面的文档.

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.

有人建议我应该使用 ListBoxFor.我已经改变了,但仍然有同样的问题.呈现选项标签时,选定的值未设置为选定值.这是我尝试过的:

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

@Html.ListBoxFor(model => model.PropertyGroups, new MultiSelectList(ViewBag.PropertyGroups, "Key", "Value"))

我已经尝试将 model.PropertyGroups 作为与值匹配的字符串集合、作为与此 ID 匹配的 Guid 集合以及作为具有键和值的匿名类型来匹配 ViewBag 中的项目.似乎没有任何效果.

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.

推荐答案

如果要创建多选列表,请不要使用 DropDownListFor.您使用 ListBoxFor 助手.

You don't use DropDownListFor if you want to create a multiselect list. You use the ListBoxFor helper.

查看模型:

public class MyViewModel
{
    public string[] SelectedIds { get; set; }
    public IEnumerable<SelectListItem> Items { get; set; }
}

控制器:

public ActionResult Index()
{
    var model = new MyViewModel
    {
        // preselect the first and the third item given their ids
        SelectedIds = new[] { "1", "3" }, 

        // fetch the items from some data source
        Items = Enumerable.Range(1, 5).Select(x => new SelectListItem
        {
            Value = x.ToString(),
            Text = "item " + x
        })
    };
    return View(model);
}

查看:

@model MyViewModel
@Html.ListBoxFor(x => x.SelectedIds, Model.Items)

这篇关于你如何正确地创建一个 MultiSelect &lt;select&gt;使用 DropdownList 助手?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆