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>
What could be the reason for the error? How can i fix the error?
Thanks in advance for your help
AJ