I’m trying to populate this IEnumerable with values from a db.
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; }
}
}
You only ever initialize a new collection of categories, there are never any added to the collection.