简体   繁体   English

ListBoxFor MultiSelectList不选择值

[英]ListBoxFor MultiSelectList does not select values

I'm incredibly confused, because this seems like it should be simple but I can't get my ListBox to be populated with selected values. 我非常困惑,因为这看起来应该很简单,但是我无法用选择的值填充ListBox I've looked at several other answers and I can't figure out what I'm doing wrong. 我查看了其他几个答案,但无法弄清楚自己在做什么错。

This is my model: 这是我的模型:

public class ItemViewModel
{
    public IEnumerable<Item> AllItems { get; set; }
    public IEnumerable<Item> SelectedItems { get; set; }
}

public class Item
{
    public string Id { get; set; }
    public string Name { get; set; }
}

This is my controller action: 这是我的控制器动作:

public ActionResult ListBoxTest()
{
    var viewModel = new ItemViewModel()
    {
        AllItems = new List<Item>()
        {
            new Item()
            {
                Id = "1",
                Name = "Name1"
            },
            new Item()
            {
                Id = "2",
                Name = "Name2"
            }
        },
        SelectedItems = new List<Item>()
        {
            new Item()
            {
                Id = "1",
                Name = "Name1"
            }
        }
    };
    return this.View(viewModel);
}

And this is the line in my view (using a default MVC View template): 这是我视图中的行(使用默认的MVC视图模板):

@Html.ListBoxFor(m => m.SelectedItems, new MultiSelectList(Model.AllItems, "Id", "Name", Model.SelectedItems.Select(s => s.Id)), new { multiple = "multiple"})

I've also tried omitting the selected values: 我也试过省略选定的值:

@Html.ListBoxFor(m => m.SelectedItems, new MultiSelectList(Model.AllItems, "Id", "Name"), new { multiple = "multiple"})

And making the selected values a list: 并将选定的值作为列表:

@Html.ListBoxFor(m => m.SelectedItems, new MultiSelectList(Model.AllItems, "Id", "Name", Model.SelectedItems.Select(s => s.Id).ToList()), new { multiple = "multiple"})

But no matter what I do, the list items are never selected. 但是,无论我做什么,都不会选择列表项。 What am I doing wrong? 我究竟做错了什么?

You cannot bind a <select> element to a collection of complex objects. 您不能将<select>元素绑定到复杂对象的集合。 A multiple select only posts back an array of simple values - the values of the selected options. 多重选择仅回发简单值的数组-所选选项的值。

Your model needs to be 您的模型需要

public class ItemViewModel
{
    public IEnumerable<Item> AllItems { get; set; }
    public IEnumerable<int> SelectedItems { get; set; }
}

and in the controller 并在控制器中

SelectedItems = new List<int>(){ 1 }

then using 然后使用

@Html.ListBoxFor(m => m.SelectedItems, new SelectList(Model.AllItems, "Id", "Name")

will select the first option in the dropdownlist. 将在下拉列表中选择第一个选项。

Note that the ListBoxFor() method sets multiple="multiple" so there is no need to set it again. 请注意, ListBoxFor()方法设置了multiple="multiple"因此无需再次设置它。 In addition, Setting the last parameter of in the SelectList() (or MultiSelectList() ) constructor is pointless. 另外,在SelectList() (或MultiSelectList() )构造函数中设置last参数是没有意义的。 Its the value of the property your binding to which determines what is selected and internally the ListBoxFor() method ignores that parameter. 它是您绑定到的属性的值,该属性确定选择了什么,并且ListBoxFor()方法在内部忽略该参数。

I would also suggest your property should be 我还建议您的财产应该是

public IEnumerable<SelectListItem> AllItems { get; set; }

so that you can simply use 这样你就可以简单地使用

@Html.ListBoxFor(m => m.SelectedItems, Model.AllItems)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM