如何显式转换类型&#39; System.Collections.Generic.List <system.web.mvc.selectlistitem>&#39;到&#39; System.Web.Mvc.SelectList&#39;?

时间:2017-06-22 19:49:06

标签: asp.net-mvc selectlist

我正在尝试创建一个新的SelectList并通过return Acctlist.ToList<SelectListItem>();语句返回它,但由于错误而无法

  

无法隐式转换类型&#39; System.Collections.Generic.List&lt; System.Web.Mvc.SelectListItem&gt;&#39;到&#39; System.Web.Mvc.SelectList&#39;

        private SelectList RemoveCustomersWithoutSites(SelectList CustomerSelectList)
    {
        // Update the Account Number dropdown in the SafeContacts pages to only include accounts that have at least one monitored location in Stages.
        SelectList CustList = null;
        List<SelectListItem> Acctlist = new List<SelectListItem>();

        foreach (SelectListItem AcntNumber in CustomerSelectList.Items){
            IEnumerable<SelectListItem> siteList = null;
            siteList = objAPICommon.GetSites(AcntNumber.ToString());

            // Add this Account if it has a site
            if (siteList != null && siteList.Count() > 0)
            {
                SelectListItem item;
                item = new SelectListItem { Text = AcntNumber.Text, Value = AcntNumber.Value };
                Acctlist.Add(item);
            }
        }

        return ***Acctlist.ToList<SelectListItem>()***;
    }

我应该如何将Acctlist投射或转换为SelectList?谢谢。

1 个答案:

答案 0 :(得分:0)

使用以下项目创建新的SelectList

new SelectList(items)