带有集合的模型 - Html.ListBoxFor 不设置所选项目 [英] Model with collection - Html.ListBoxFor doesn't set selected items

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

问题描述

这个让我发疯,在我失去理智之前请帮忙.

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

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

以下是我的模型

公共类线程{公共列表<ForumMessage>消息 { 得到;放;}//来自数据库的主列表公共列表<分类>AllClassifications { 得到;放;}公共字符串主题{获取;放;}公共 int[] ThreadSelectedClassifications { 获取;放;}}公开课ForumMessage{公共字符串消息名称 { 获取;放;}public int[] SelectedClassifications { get;放;}}公开课分类{公共 int ID { 获取;放;}公共字符串标题{获取;放;}公共字符串 描述 { 获取;放;}}

下面是我的控制器

public ActionResult Index(){var 线程 = 新线程();thread.Messages = new List();thread.AllClassifications = new List(){新分类 { ID = 1, Title = "One" , Description = "One" },新分类 { ID = 2, Title = "Two" , Description = "Two" },新分类 { ID = 3, Title = "Three" , Description = "Three" },新分类 { ID = 4, Title = "Four" , Description = "Four" },新分类 { 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);}返回视图(线程);

以下是我的观点

@model MultiSelectDemo.Controllers.Thread@foreach(Model.Messages 中的 var 项){<div class="row"><div class="col-md-4">@*@不设置选中的项目*@@Html.ListBoxFor(m => item.SelectedClassifications, new SelectList(Model.AllClassifications, "ID", "Title"));

<div class="col-md-4">@* 不设置选中的项目 *@@Html.ListBoxFor(m => item.SelectedClassifications, new SelectList(Model.AllClassifications, "ID", "Title", item.SelectedClassifications));

<div class="col-md-4">@* 不设置选中的项目 *@@Html.DropDownListFor(m => item.SelectedClassifications, new SelectList(Model.AllClassifications, "ID", "Title", item.SelectedClassifications), new { @multiple = "multiple" })

}<小时/><div class="row"><div class="col-md-12">这有效.@Html.ListBoxFor(m => m.ThreadSelectedClassifications, new MultiSelectList(Model.AllClassifications, "ID", "Title"))

}

解决方案

SelectList 仅支持单个选定值,因此在您的代码示例中,您告诉它将选定值设置为选项它具有整个选定值列表的值.因为不存在这样的选项,所以没有选择.

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

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

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

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.

Below are my models

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 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 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屋!

查看全文
相关文章
C#/.NET最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆