从模型中的多个列表中选择并将其放入下拉列表中

问题描述

我正在尝试通过此操作填充我的下拉列表

  var componentList = (from c in model.Components
                                 select new
                                 {
                                     compID = c.ID,compName = c.CompID + ":" + c.ComponentName
                                 }).OrderBy(x => x.compName).ToList();
                      (from sc in model.ComponentSubComps
                                select new
                                {
                                     compID = sc.ID,compName = sc.SubCompID + ":" + sc.SubCompName
                                }).OrderBy(x => x.compName).ToList();

  ViewBag.Components = new SelectList(componentList,"compID","compName");

但这只是从模型的model.Components列表中提取数据。我如何才能做到这一点,使其也从model.ComponentSubComps列表中拉出来?

解决方法

我不知道您是否要组合组件和子组件。假定它们在数据库中的单独表中并且它们的主键只是增量整数值,则将它们组合在一起是混合ID的潜在危险。

如果不首先注意的话,这将成为一个严重的错误。如果它们的ID可以相同,则可能必须使用两个下拉列表(一个下拉列表用于组件,一个下拉列表用于子组件),而不是一个。

如果您必须将两者结合起来,听起来好像几乎必须将某些东西与ID唯一地结合在一起,将它们分配为不同的组,并在收到发回邮件时解析ID:

// Define the Groups
var componentGroup = new SelectListGroup { Name = "Component" };
var subComponentGroup = new SelectListGroup { Name = "SubComponent" };

var componentOptions = model.Components
    .OrderBy(x => x.ComponentName)
    .Select(x => new SelectListItem
    {
        Value = "c:" + x.CompID,Text = x.CompID + ":" + x.ComponentName,Group = componentGroup 
    });

var subComponentOptions = model.ComponentSubComps
    .OrderBy(x => x.SubCompName)
    .Select(x => new SelectListItem
    {
        Value = "sc:" + x.SubCompID,Text = x.SubCompID + ":" + x.SubCompName,Group = subComponentGroup
    });

var dropdownOptions = new List<SelectListItem>();
dropdownOptions.AddRange(componentOptions);
dropdownOptions.AddRange(subComponentOptions);

然后,当用户进行选择并将下拉值回发到服务器时,您可能必须解析所选的下拉值,并查看ID是属于组件还是子组件。


如果组件和子组件之间没有区别,那么您可以轻松地将它们组合起来:

var componentOptions = model.Components
    .OrderBy(x => x.ComponentName)
    .Select(x => new SelectListItem
    {
        Value = x.CompID,Text = x.CompID + ":" + x.ComponentName
    });

var subComponentOptions = model.ComponentSubComps
    .OrderBy(x => x.SubCompName)
    .Select(x => new SelectListItem
    {
        Value = x.SubCompID,Text = x.SubCompID + ":" + x.SubCompName
    });

// This is your SelectList. I would highly recommend you build a strongly-typed
// view model and use it instead of ViewBag.
var dropdownOptions = new List<SelectListItem>();
dropdownOptions.AddRange(componentOptions);
dropdownOptions.AddRange(subComponentOptions);
,

您有两个陈述。您存储在componentList中的第一条语句的结果,但是第二条语句的结果没有存储在任何地方。

尝试将结果 第二陈述添加到componentList。>

可能List<T>.AddRange(IEnumerable<T>)是最好的选择。

编辑:

var componentList = (from c in model.Components
                             select new
                             {
                                 compID = c.ID,compName = c.CompID + ":" + c.ComponentName
                             }).OrderBy(x => x.compName).ToList();

var secondResult = (from sc in model.ComponentSubComps
                            select new
                            {
                                 compID = sc.ID,compName = sc.SubCompID + ":" + sc.SubCompName
                            }).OrderBy(x => x.compName).ToList();

componentList.AddRange(secondResult);

ViewBag.Components = new SelectList(componentList,"compID","compName");
,

您可以使用实体编写查询并获取:

var componentList = model.Components.Select(c => new Components
{
    compID = c.ID,compName = c.CompID + ":" + c.ComponentName
}).OrderBy(x => x.compName).ToList();

var componentList1 = model.ComponentSubComps.Select(c => new Components
{
    compID = c.ID,compName = c.SubCompID + ":" + c.SubCompName
}).OrderBy(x => x.compName).ToList();

componentList.AddRange(componentList1);
ViewBag.Components = new SelectList(componentList,"compName");