List<System.Drawing.Color>
正如我所提到的,我有一个带有 的模型,它可以Seed()
像这样使用:
protected override void Seed(DatabaseContext context)
{
var somethings = new List<Something>
{
new Something
{
Name="blah blah", Colors= { Color.Black, Color.Red }
}
};
}
只要我有Colors
那样的那边,我总是
Object reference not set to an instance of an object.
在 line收到var somethings = new List<Something>
。
当我删除 时Colors
,它消失了,一切正常,是什么原因造成的,我该如何解决这个问题?
谢谢。
编辑:
Something
的型号:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
namespace MVCApplication7
{
public class Something
{
public int SomethingID { get; set; }
public string Name { get; set; }
public List<Color> Colors { get; set; }
}
}
控制器:
........
private DatabaseContext db = new DatabaseContext();
//
// GET: /Home/
public ViewResult Index()
{
return View(db.Somethings.ToList());
}
查看:(我确信它是无关紧要的,因为调试器显示它Colors
是空的。
@foreach (var itemColor in item.Colors)
{
Html.Raw(itemColor.ToString());
}
全球.asax
.........
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
Database.SetInitializer<DatabaseContext>(new DatabaseInitializer());
}