【问题标题】:Model with collection - Html.ListBoxFor doesn't set selected items带有集合的模型 - Html.ListBoxFor 未设置所选项目
【发布时间】:2015-01-30 00:09:51
【问题描述】:

这个让我快疯了,在我失去理智之前请帮忙。

问题总结: 我的模型“线程”有“论坛消息”的集合,每个论坛消息都有一个多选下拉列表。我要做的就是根据来自数据库的值设置选定的值。我已经通过了很多线程,但无法找到解决方案。

如果您知道任何此类问题,请告诉我,我会一一解答。

以下是我的模型

public class Thread
{
    public List<ForumMessage> Messages { get; set; }
    //Master list coming from DB
    public List<Classifications> AllClassifications { get; set; }
    public string Subject { get; set; }
    public int[] ThreadSelectedClassifications { get; set; }
}

public class ForumMessage
{
    public string MessageName { get; set; }
    public int[] SelectedClassifications { get; set; }
}

public class Classifications
{
    public int ID { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
}

下面是我的控制器

public ActionResult Index()
{
    var thread = new Thread();
    thread.Messages = new List<ForumMessage>();

    thread.AllClassifications = new List<Classifications>() 
    { 
        new Classifications { ID = 1, Title = "One"   , Description = "One"   }, 
        new Classifications { ID = 2, Title = "Two"   , Description = "Two"   }, 
        new Classifications { ID = 3, Title = "Three" , Description = "Three" }, 
        new Classifications { ID = 4, Title = "Four"  , Description = "Four"  }, 
        new Classifications { ID = 5, Title = "Five"  , Description = "Five"  } 
    };
    thread.ThreadSelectedClassifications = new int[] { 2, 4 };
    for (int i = 0; i < 5; i++)
    {
        var post = new ForumMessage();
        post.SelectedClassifications = new int[] { 2, 4 };
        post.MessageName = i.ToString();
        thread.Messages.Add(post);
    }

    return View(thread);    

以下是我的看法

@model MultiSelectDemo.Controllers.Thread
@foreach (var item in Model.Messages)
{
    <div class="row">
        <div class="col-md-4">
            @*@Doesn't set the items selected *@
            @Html.ListBoxFor(m => item.SelectedClassifications, new SelectList(Model.AllClassifications, "ID", "Title"));
        </div>

        <div class="col-md-4">
            @* Doesn't set the items selected  *@
            @Html.ListBoxFor(m => item.SelectedClassifications, new SelectList(Model.AllClassifications, "ID", "Title", item.SelectedClassifications));
        </div>
        <div class="col-md-4">
            @* Doesn't set the items selected  *@
            @Html.DropDownListFor(m => item.SelectedClassifications, new SelectList(Model.AllClassifications, "ID", "Title", item.SelectedClassifications), new { @multiple = "multiple" })
        </div>
    </div>
}
<hr />
<div class="row">
    <div class="col-md-12">
        This works.
        @Html.ListBoxFor(m => m.ThreadSelectedClassifications, new MultiSelectList(Model.AllClassifications, "ID", "Title"))
    </div>
</div>

}

【问题讨论】:

    标签: c# asp.net-mvc-5 .net-4.5 html.dropdownlistfor html.listboxfor


    【解决方案1】:

    SelectList 仅支持单个选定值,因此在您的代码示例中,您告诉它将选定值设置为具有整个选定值列表的值的选项。由于不存在此类选项,因此未选择任何内容。

    MultiSelectList 另一方面,允许传递选定值的可枚举。如果你使用正确的类,你会没事的。

    @Html.ListBoxFor(m => item.SelectedClassifications, new MultiSelectList(Model.AllClassifications, "ID", "Title", item.SelectedClassifications))
    

    【讨论】:

    • 对不起,我也应该在我的示例中添加这一行,如果你看到我的最后一行,这正是我正在使用的,但不知何故它在集合中不起作用,所以我认为是缺少一些东西。
    • 是的,很抱歉。我从您的代码中复制了该行,打算只添加“Multi”位,但我复制了错误的行(您没有传递选定的值)。
    • 经过一番困惑,我发现“选定”项目的列表必须是一个 ID 数组。因此,如果您有一个选定项目的集合: int[] selectedItemsToPassAsParameter = selectedItems.Select(item => item.ItemId).ToArray()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-25
    • 2012-12-02
    • 2011-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-16
    相关资源
    最近更新 更多