与采集模式 - Html.ListBoxFor不设置选定的项目 [英] Model with collection - Html.ListBoxFor doesn't set selected items

查看:216
本文介绍了与采集模式 - Html.ListBoxFor不设置选定的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这一个是我逼疯之前,我失去理智的我,请帮助。

This one is driving me crazy and before I loose my sane please help.

问题的总结:
我的模型线程有ForumMessage的集合,每个ForumMessage有一个多选择下拉列表。所有我想要做的就是设置基于从数据库中值来选定的值。我已经直通多个线程,但没能找到解决方案。

Summary of the issue: My model "Thread" has collection of "ForumMessage", each ForumMessage has one Multi select drop down list. All I want to do is set the selected value based on values coming from Database. I have gone thru many threads but wasn't able to find the solution.

在,如果你知道任何这类问题的,请让我知道,我会去他们直通案例

In case if you are aware of any such question please let me know and I will go thru them.

下面是我的模型

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

下面是我的控制器

Below is my controller

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

下面是我的看法。

Below is my view

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

}

}

推荐答案

的SelectList 它设置所选择的值作为具有所选值的整个列表的值的选项。由于没有这样的选项存在,没有被选中。

SelectList only supports a single selected value, so in your code examples you're telling it to set the selected value as the option which has a value of the entire list of selected values. Since no such option exists, nothing is selected.

MultiSelectList ,另一方面,允许通过选定值的枚举。如果你使用正确的类,你会好起来的。

MultiSelectList on the other hand, allows passing an enumerable of selected values. If you use the right class, you'll be okay.

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

这篇关于与采集模式 - Html.ListBoxFor不设置选定的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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