开发者

c# - MVC Error: Object reference not set to an instance of an object

开发者 https://www.devze.com 2023-02-08 12:21 出处:网络
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:

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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消