ListBoxFor MultiSelectList不选择值 [英] ListBoxFor MultiSelectList does not select values

查看:102
本文介绍了ListBoxFor MultiSelectList不选择值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常困惑,因为这看起来应该很简单,但是我无法用选择的值填充ListBox.我已经查看了其他几个答案,但无法弄清楚自己在做什么错.

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. I've looked at several other answers and I can't figure out what I'm doing wrong.

这是我的模特

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

这是我的控制器操作:

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

这是我视图中的行(使用默认的MVC View模板):

And this is the line in my view (using a default MVC View template):

@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?

推荐答案

您不能将<select>元素绑定到复杂对象的集合.多项选择仅回发简单值的数组-所选选项的值.

You cannot bind a <select> element to a collection of complex objects. A multiple select only posts back an array of simple values - the values of the selected options.

您的模型必须是

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

并在控制器中

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

然后使用

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

将在下拉列表中选择第一个选项.

will select the first option in the dropdownlist.

请注意,ListBoxFor()方法设置了multiple="multiple",因此无需再次设置.此外,在SelectList()(或MultiSelectList())构造函数中设置last参数是没有意义的.它是您绑定到的属性的值,该属性决定了要选择的内容,并且ListBoxFor()方法在内部会忽略该参数.

Note that the ListBoxFor() method sets multiple="multiple" so there is no need to set it again. In addition, Setting the last parameter of in the SelectList() (or MultiSelectList()) constructor is pointless. Its the value of the property your binding to which determines what is selected and internally the ListBoxFor() method ignores that parameter.

我也建议您的财产应该是

I would also suggest your property should be

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

以便您可以简单地使用

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

这篇关于ListBoxFor MultiSelectList不选择值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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