MVC MusicStore Artist.Name对象引用不设置到对象的实例 [英] MVC MusicStore Artist.Name Object reference not set to an instance of an object

查看:166
本文介绍了MVC MusicStore Artist.Name对象引用不设置到对象的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

破解我的头,同时通过MVC MusicStore学习MVC。得到这个错误的Model.Artist.Name的详细信息查看页面。

我的Storecontroller详细方法应该罚款。

 公众的ActionResult详细信息(INT ID)
    {
        //返回和专辑从ID搜索
        VAR专辑= storeDB.Albums.Find(ID);
        返回查看(专辑);
    }
 

这是我怎么输出的看法

 <李>价格:<%= Model.Price%>< /李>
    <李>艺术家:<%= Model.Artist.Name%>< /李>
 

工作正常的价格,而且只显示错误Model.Genre.Name和Artist.Name。我怀疑在声明中sampledata.cs这些特性导致出现此问题

 艺术家= artists.Single(A => a.Name ==柯普兰和放大器;伦敦交响乐团)
 

但我这方面的知识太薄弱了,补什么。请帮忙 。

好了,该值是从获取类文件看起来像这样

 保护覆盖无效种子(MusicStoreEntities上下文)
    {
        VAR艺术家=新的名单,其中,艺术家>
        {
            新人{名称=柯普兰和放大器;伦敦交响乐团},
            etcetc
        }
         新名单,其中,相册和GT;
        {
            新专辑{标题=ABC,艺术家= artists.Single(G => g.Name ==测试)}
        }
    }
 

以怎样的价值被转让的通知,我可以访问Model.Title罚款(其中型号为短手),但Model.Artist.Name使我的错误。

解决

好了,把它加入到工作的虚拟关键字在Album类的艺术家和流派声明。但我仍然不知道发生了什么,并希望有任何人照顾摆脱一些灯。

在我的专辑类,它解决之前,它看起来像这样

 公共类专辑
{
    公众诠释ALBUMID {获得;组; }
    公众诠释GenreId {获得;组; }
    公众诠释ArtistId {获得;组; }
    公共字符串名称{获取;组; }
    公共十进制价格{获得;组; }
    公共字符串AlbumArtUrl {获得;组; }

    公共体裁类型{获取;组; }
    公共艺术家艺术家{获得;组; }
}
 

引起的流派和艺术家的错误是通过添加虚拟关键字解决

  public虚拟艺术家艺术家{获得;组; }
 

不知怎的,我仍然不能证明发生了什么,并希望了解更多信息。有人在乎解释?

它是这样的,Album.cs

 命名空间MvcMusicStore.Models
{
    公共类专辑
    {
        公众诠释ALBUMID {获得;组; }
        公众诠释GenreId {获得;组; }
        公众诠释ArtistId {获得;组; }
        公共字符串名称{获取;组; }

        公共虚拟体裁类型{获取;组; }
        公共虚拟艺术家艺术家{获得;组; }
    }
}
 

使用EF MusicStore.cs

 命名空间MvcMusicStore.Models
{
    //重新present实体框架,处理创建,读取,更新和删除OPS
    公共类MusicStoreEntities:的DbContext
    {
        公共DbSet<相册和GT;专辑{获得;组; }
        公共DbSet<类型>流派{获得;组; }
        公共DbSet<艺术家>艺术家{获得;组; }

    }
}
 

和在StoreController.cs实施

  MusicStoreEntities storeDB =新MusicStoreEntities();

    公众的ActionResult详细信息(INT ID)
    {
        //返回和专辑从ID搜索
        VAR专辑= storeDB.Albums.Find(ID);
        返回查看(专辑);
    }
 

最后的样本数据。

 保护覆盖无效种子(MusicStoreEntities上下文)
{
    VAR艺术家=新的名单,其中,艺术家>
    {
        新人{名称=柯普兰和放大器;伦敦交响乐团},
        etcetc
    }
     新名单,其中,相册和GT;
    {
        新专辑{标题=ABC,艺术家= artists.Single(G => g.Name ==测试)}
    }
}
 

解决方案

好了,把它加入虚拟关键字在Album类的艺术家和流派申报工作。但我仍然不知道发生了什么,并希望有任何人照顾摆脱一些灯。

见上面。

Cracking my head while learning MVC through MVC MusicStore . Got this error for Model.Artist.Name for Details View page .

My Storecontroller Details method should be fine .

    public ActionResult Details(int id)
    {
        //returns and albums searched from the id 
        var albums = storeDB.Albums.Find(id);
        return View(albums);
    }

And this is how I output the view

    <li>Price : <%=Model.Price %></li>
    <li>Artist : <%=Model.Artist.Name%></li>

Works fine for the price , and it only show error for Model.Genre.Name and Artist.Name . I am suspecting the declaration for these properties in sampledata.cs caused this issue

    Artist = artists.Single(a => a.Name == "Aaron Copland & London Symphony Orchestra")

But my knowledge of this is too weak to make up anything . Please help .

Alright , the value is fetched from a class file looking like this

    protected override void Seed(MusicStoreEntities context)
    {
        var artists = new List<Artist>
        {
            new Artist { Name = "Aaron Copland & London Symphony Orchestra" },
            etcetc
        }
         new List<Album>
        {
            new Album { Title = "ABC", Artist= artists.Single(g => g.Name == "Test")}
        }
    }

Take notice on how the value are being assigned , I can access Model.Title fine (where Model as the short hand) , but Model.Artist.Name caused me that error .

Solved

Alright , got it to work by adding virtual keyword to the Artist and Genre declaration in Album class . But I am still not sure what happens and would like to have anyone care to shed some lights .

In my Album class , before it is solved it looks like this

public class Album
{
    public int AlbumId { get; set; }
    public int GenreId { get; set; }
    public int ArtistId { get; set; }
    public string Title { get; set; }
    public decimal Price { get; set; }
    public string AlbumArtUrl { get; set; }

    public Genre Genre { get; set; }
    public Artist Artist { get; set; }
}

The error caused by Genre and Artist is solved by adding Virtual keyword

    public virtual Artist Artist { get; set; }

Somehow , I still couldn't justify what happens and wish to learn more . Anyone care to explain ?

It goes like this , Album.cs

namespace MvcMusicStore.Models
{
    public class Album
    {
        public int AlbumId { get; set; }
        public int GenreId { get; set; }
        public int ArtistId { get; set; }
        public string Title { get; set; }

        public virtual Genre Genre { get; set; }
        public virtual Artist Artist { get; set; }
    }
}

using EF MusicStore.cs

namespace MvcMusicStore.Models
{
    //represent entity framework , handle create ,read , update and del ops 
    public class MusicStoreEntities : DbContext
    {
        public DbSet<Album> Albums { get; set; }
        public DbSet<Genre> Genres { get; set; }
        public DbSet<Artist> Artists { get; set; }

    }
}

and implemented in StoreController.cs

    MusicStoreEntities storeDB = new MusicStoreEntities();

    public ActionResult Details(int id)
    {
        //returns and albums searched from the id 
        var albums = storeDB.Albums.Find(id);
        return View(albums);
    }

Finally the sample data

protected override void Seed(MusicStoreEntities context)
{
    var artists = new List<Artist>
    {
        new Artist { Name = "Aaron Copland & London Symphony Orchestra" },
        etcetc
    }
     new List<Album>
    {
        new Album { Title = "ABC", Artist= artists.Single(g => g.Name == "Test")}
    }
}

解决方案

Alright , got it to work by adding virtual keyword to the Artist and Genre declaration in Album class . But I am still not sure what happens and would like to have anyone care to shed some lights .

See above .

这篇关于MVC MusicStore Artist.Name对象引用不设置到对象的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆