Click here to Skip to main content
16,021,823 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
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

C#
@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>


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

Thanks in advance for your help
AJ
Posted

1 solution

Such errors are terrible. Sometimes it helps to do a "clean all" follwoed by a "rebuild all".
 
Share this answer
 
Comments
eajay 21-Mar-12 20:25pm    
Tried doing the same, still no luck, Same error.
Please help.
Thanks
AJ

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900