问题描述:

I'm using MVC 5 and following the MVC 4 Music Store tutorial, since one doesn't seem to exist for the latest version.

I've gotten to the point in the tutorial where a StoreIndexViewModel and related controller and view have been added.

When I run it though, and hit the Store page, it's throwing the following:

*Error 1 'System.Collections.Generic.IEnumerable' does not contain a definition for 'genres' and no extension method 'genres' accepting a first argument of type 'System.Collections.Generic.IEnumerable' could be found (are you missing a using directive or an assembly reference?) c:\Users\abourke\dev\csdev\Training\MVC5\MvcMusicStore\MvcMusicStore\Views\Store\Index.cshtml 9 23 MvcMusicStore

*

Here's the StoreIndexViewModel:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

namespace MvcMusicStore.ViewModels

{

public class StoreIndexViewModel

{

public int NumberOfGenres { get; set; }

public List<string> Genres { get; set; }

}

}

And the Index() method of the related StoreController:

//

// GET: /Store/

public ActionResult Index()

{

// Create a list of genres

var genres = new List<string> { "Rock", "Jazz", "Country", "Pop", "Disco" };

// Create our view model

var viewModel = new StoreIndexViewModel

{

NumberOfGenres = genres.Count(),

Genres = genres

};

return View(viewModel);

}

And finally the Index.chstml view:

 @model IEnumerable<MvcMusicStore.ViewModels.StoreIndexViewModel>

@{

ViewBag.Title = "Browse Genres";

}

<h2>Browse Genres</h2>

<p>Select from @Model.genres</p>

<ul>

@foreach (string genreName in Model.Genres)

{

<li>

@genreName

</li>

}

</ul>

网友答案:

At first, your view is typed with different type, should be

@model MvcMusicStore.ViewModels.StoreIndexViewModel

At second, I assume the "Select from..." string should look like this:

<p>Select from @Model.NumberOfGenres</p>
相关阅读:
Top