获取System.InvalidOperationException错误 [英] Getting System.InvalidOperationException Error

查看:82
本文介绍了获取System.InvalidOperationException错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

尝试从我的视图查看详细信息时出现以下错误

传递到字典中的模型项的类型为"System.Collections.Generic.List`1 [MVCMusicStore.Models.Genre]",但此字典需要类型为"MVCMusicStore.Models.Genre"的模型项.

我正在使用MVC音乐商店应用程序,但正在使用企业库进行数据访问

以下是我的模型的代码

Hello,

I am getting the below error while try to view the details from my View

The model item passed into the dictionary is of type ''System.Collections.Generic.List`1[MVCMusicStore.Models.Genre]'', but this dictionary requires a model item of type ''MVCMusicStore.Models.Genre''.

I am using MVC Music Store application but i am using Enterprise Library for my Data access

Below is the code fro my Model

public List<Genre> GetAlbumByGenreName(string genre)
        {
            IList<Genre> genres = new List<Genre>();
            
            Database db = DatabaseFactory.CreateDatabase();
            DbConnection cn = db.CreateConnection();
            cn.Open();

            DbCommand cmd = cn.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "Select GenreId,Name,Description from Genre Where Name = '" + genre + "'";
            using (SqlDataReader dr = cmd.ExecuteReader() as SqlDataReader)
            {
                while (dr.Read())
                {
                    IList<Album> albums = new List<Album>();
                    DbConnection cnAlbum = db.CreateConnection();
                    cnAlbum.Open();
                    DbCommand cmdAlbum = cnAlbum.CreateCommand();
                    cmdAlbum.CommandType = CommandType.Text;
                    cmdAlbum.CommandText = "Select AlbumId,GenreId,ArtistId,Title,Price,AlbumArtUrl from Album Where GenreId =" + dr.GetInt32(0).ToString();

                    using (SqlDataReader drAlbum = cmdAlbum.ExecuteReader() as SqlDataReader)
                    {
                        while (drAlbum.Read())
                        {
                            albums.Add(new Album
                                {
                                    ArtistId = drAlbum.GetInt32(2),
                                    AlbumId = drAlbum.GetInt32(0),
                                    GenreId = drAlbum.GetInt32(1),
                                    Title = drAlbum.GetString(3),
                                    Price = drAlbum.GetDecimal(4),
                                    AlbumArtUrl = drAlbum.GetString(5)
                                });
                        }
                    }
                    cnAlbum.Close();
                    genres.Add(new Genre
                    {
                        GenreId = dr.GetInt32(0),
                        Name = dr.GetString(1),
                        Description = dr.GetString(2),
                        Albums = albums.ToList()
                    });
                }
            }
            cn.Close();
            return genres.ToList();



下面是我的视图的代码



And below is the code for my View

@model MVCMusicStore.Models.Genre

@{
    ViewBag.Title = "Browse";
}

<h2>Browsing Genre: @Model.Name</h2>

<ul>
    @foreach (var album in Model.Albums)
    {
        <li>
            @album.Title
        </li>
    }
</ul>



错误的原因可能是什么?我该如何解决错误?

预先感谢您的帮助
AJ



What could be the reason for the error? How can i fix the error?

Thanks in advance for your help
AJ

推荐答案

这样的错误很可怕.有时它有助于完成全部重建"之后的全部清理".
Such errors are terrible. Sometimes it helps to do a "clean all" follwoed by a "rebuild all".


这篇关于获取System.InvalidOperationException错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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