带集合的模型 - Html.ListBoxFor不设置所选项目

时间:2014-12-01 21:30:02

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

这个让我发疯,在我放松了我的理智之前请帮助。

问题摘要: 我的模特"线程"拥有" ForumMessage"的集合,每个ForumMessage都有一个多选下拉列表。我想要做的就是根据来自数据库的值设置所选值。我已经通过很多线程,但无法找到解决方案。

如果您知道任何此类问题,请告诉我,我会通过他们。

以下是我的模特

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>

Sample Output}

1 个答案:

答案 0 :(得分:7)

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

另一方面,

MultiSelectList允许传递可枚举的选定值。如果你使用正确的班级,你会没事的。

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