[英]why do I only get nulls in this IEnumerable<>
I'm trying to populate this IEnumerable with values from a db. 我试图用数据库中的值填充此IEnumerable。
I can't see what I'm doing wrong. 我看不到我在做什么错。
IEnumerable<Categories> categories = new List<Categories>();
List<SelectListItem> catItems = new List<SelectListItem>();
foreach (var cats in categories)
{
catItems.Add(new SelectListItem
{
Text = cats.CategoryName,
Value = cats.CategoryID.ToString()
});
}
Here is the class: 这是课程:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
namespace SportsStore.Domain.Entities
{
public class Categories
{
[Key]
public int CategoryID { get; set; }
public string CategoryName { get; set; }
}
}
您只需初始化一个新的类别集合,就不会添加任何类别。
How are you populating the categories list? 您如何填充类别列表? It won't just get filled by saying...
不仅仅是说...
IEnumerable<Categories> categories = new List<Categories>();
You need populate it somehow. 您需要以某种方式填充它。
Because here you create a new (and empty) list of Categories: 因为在这里您创建了一个新的(且为空)类别列表:
IEnumerable<Categories> categories = new List<Categories>();
and here you try to iterate over the empty list: 在这里,您尝试遍历空列表:
foreach (var cats in categories)
{
Which means you're never adding anything to cat.Items 这意味着您永远不会向cat.Items添加任何内容
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.