0

I am trying to get started learning MVC. I built the MVC Music Store project that I found on the Microsoft site. http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-1.

I got it working pretty well but I ran into trouble when I tried to modified it. I want to put the data in a separate project. I used Linq to Entities for the data access.

Here is my class to access the data

public class clsUtilities
{
    Utilities.MVCMusicStoreEntities db = new Utilities.MVCMusicStoreEntities();

    public object GetAlbums(string GenreName)
    {
        var query = from tags in db.vieAlbumArtists
                    where tags.GenreName.Equals(GenreName)
                    select tags;

        return query;
    }
}

In my Controller my code is

public ActionResult Browse2(string genre)
{
    // retrieve Genre and its associated albums from the database

    var genreModel = mcloUtilities.GetAlbums(genre);

    return View(genreModel);
}

I generate a cshtml file in my view

@model Utilities.vieAlbumArtist

@{
    ViewBag.Title = "Browse2";
}

This all compiles ok but when I run it I get:

The model item passed into the dictionary is of type System.Data.Objects.ObjectQuery'1[Utilities.vieAlbumArtist], but this dictionary requires a model item of type Utilities.vieAlbumArtist.

4

4 回答 4

1

改变

@model Utilities.vieAlbumArtist

@model IEnumerable<Utilities.vieAlbumArtist>

您正在返回一个列表vieAlbumArtist而不仅仅是一个实例。

你可能还想改变

public object GetAlbums(string GenreName)

public IEnumerable<Utilities.vieAlbumArtist> GetAlbums(string GenreName)

绕过objects不是最好的主意。

于 2012-05-24T22:09:34.467 回答
0

这是你的问题:

public object GetAlbums(string GenreName)

您将数据作为对象返回。但是视图期望接收一个类型的项目Utilities.vieAlbumArtist。要么更改GetAlbums()方法以返回适当的类型,要么确保在控制器中将其转换为视图期望的类型。

于 2012-05-24T22:09:16.437 回答
0

在你们给我的提示的帮助下,我能够解决这个问题。

来自 clsUtilies

  public class clsUtilities
    {
        Utilities.MVCMusicStoreEntities db = new Utilities.MVCMusicStoreEntities();

        public IEnumerable<Utilities.vieAlbumArtist> GetAlbums(string GenreName)
        {




        IEnumerable<vieAlbumArtist> query = from tags in db.vieAlbumArtists
                    where tags.GenreName.Equals(GenreName)

                    select tags;

            foreach (var n in query)
            {

            }

        return query;

        }

从控制器

 public ActionResult Browse2(string genre)
        {
            // retrieve Genre and its associated albums from the database


            var genreModel = mcloUtilities.GetAlbums(genre);


            return View(genreModel );
        }

从意见

@model IEnumerable< Utilities.vieAlbumArtist> 


@{
    ViewBag.Title = "Browse Albums";
}





@foreach(var album in Model) 
{ 
   <ul>  
       <a href="@Url.Action("Details", new { id=album.AlbumId})">
       <img alt="@album.Title" src="@album.AlbumArtUrl" />
       <span>@album.Title (   @album.Name ) </span> </a>
       </ul>
} 











}
于 2012-06-04T19:33:00.237 回答
-1

要显示专辑列表,请尝试以下操作:

<ul>
@foreach(var item in vieAlbumArtist)
{
   <li>item.Name</li> //Not sure if you actually have a name property
}
</ul>
于 2012-05-24T22:10:20.600 回答