简体   繁体   English

在多选列表框中显示选定的项目

[英]Show selected items in a multi-select list box

I have tried various ways to get this to work. 我尝试了各种方法来使它起作用。 I think maybe I have something missing in my model or controller, so I'm posting all three parts. 我认为我的模型或控制器中可能缺少某些内容,因此我将发布所有这三个部分。

I have data in a database that shows some advising topics chosen by an advisor in a previous appointment. 我的数据库中的数据显示了顾问在先前约会中选择的一些建议性话题。 When the advisor calls up that appointment, the app should display a list of all possible topics with the ones previously chosen highlighted. 当顾问调用该约会时,应用程序应显示所有可能主题的列表,并突出显示先前选择的主题。 Everything works except that last bit. 除了最后一点,一切正常。

I know I'm retrieving the right information because I can display the selected items separately. 我知道我正在检索正确的信息,因为我可以分别显示所选的项目。 I just can't get them to show up selected. 我只是无法让他们出现而已。 Here's the code. 这是代码。 I'm cutting out irrelevant parts. 我删去了不相关的部分。

public class AppointmentModel
{ ...
    public string AdvisingTopicId { get; set; }
    public List<SelectListItem> AdvisingIdList { get; set; }
    public SelectList AdvisingTopicNames { get; set; }
}

public class HomeController : AdvisorBaseController
{ ...
        var topicCodes = appointment.advising_topic.ToList();

        var advisingTopics = new SelectList((from t in topicCodes
                              select t.name).ToList(), "name");

        var topicsList = (from t in db.advising_topic
                          select new SelectListItem
                          {
                              Selected = false,
                              Text = t.name,
                              Value = SqlFunctions.StringConvert((double)t.advising_topic_id).Trim()
                          }).ToList();

        foreach (var topicCode in topicCodes)
        {
            var selTopic = topicsList.Find(x => x.Value == topicCode.advising_topic_id.ToString());
            if (selTopic != null)
            {
                selTopic.Selected = true;
            }
        } ...
            var appointmentModel = new AppointmentModel
            { ...
                AdvisingTopicNames = advisingTopics,
                AdvisingIdList = topicsList,
            };

and then the view 然后查看

@model AcademicAdvising.Models.AppointmentModel
<h3>Advising Topics</h3>
<ul>
    @foreach (var item in Model.AdvisingTopicNames)
    {
        <li>@Html.DisplayFor(x => item)</li>
    }
</ul>
    @Html.ListBoxFor(m=>m.AdvisingIdList, new SelectList(Model.AdvisingTopicNames, "Value", "Text", Model.AdvisingTopicNames.SelectedValue))

Note that the foreach correctly displays the selected items. 请注意,foreach可以正确显示所选项目。 That's just for testing and will be pulled out. 那只是为了测试,将被淘汰。 the ListBoxFor is where I'm struggling. ListBoxFor是我苦苦挣扎的地方。 What I have here doesn't work (shows the full list with nothing highlighted). 我在这里没有用(显示完整列表,但未突出显示任何内容)。 And that's the bit where I have tried various approaches, with all fails. 这就是我尝试各种方法但都失败的地方。

It looks like you may have accidentely went a level too deep. 看来您可能不小心将水平调得太深。 You already have a select list which is all the listboxfor function wants. 您已经有一个选择列表,这是listboxfor函数所需的全部。

 @Html.ListBoxFor(m=>m.AdvisingIdList, Model.AdvisingTopicNames)

But honestly looking at how you are defining your lists I think what you really want might be 但说实话,看看您如何定义列表,我想您真正想要的是

 @Html.ListBoxFor(m=>m.AdvisingIdList, Model.AdvisingIdList)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM