c# - MVC Error: Object reference not set to an instance of an object
I am as many others following the MVC Music Store Tutorial, but I'm stuck with an error.
At page 48, the tutorial says to write an Action开发者_如何学GoResult-view:
public ActionResult Index()
{
var genres = storeDb.Genres.ToList()
return View(genres);
}
but I get an error on genres. Visual Web Developer says "value cannot be null".
what should I set genres to? var genres = new ??
Thank you!
Make sure you have specified a connection string to your database in web.config. Also make sure you initialize the storeDb
variable before using it:
public ActionResult Index()
{
var storeDb = new StoreDbDataContext(); // Replace this with the actual type
var genres = storeDb.Genres.ToList();
return View(genres);
}
精彩评论