如何正确创建 MultiSelect

发布于 2024-12-20 05:50:59 字数 1630 浏览 0 评论 0原文

(抱歉,这里有几个项目,但似乎没有一个项目允许我完成这项工作。)

我想创建一个允许多项选择的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我闹别瞎闹 2024-12-27 05:50:59

如果您想创建多选列表,则不要使用 DropDownListFor。您使用 ListBoxFor 帮助器。

视图模型:

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)

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

View model:

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

Controller:

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

View:

@model MyViewModel
@Html.ListBoxFor(x => x.SelectedIds, Model.Items)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文