开发者

Error while running Mvc Music store

First of all I am sorry to ask a stupid question, this is because I am novice in ASP.NET. I am following asp.net MVC music store tutorial and here is what I have made:

 namespace MvcMusicStore.Controllers
{
    public class StoreController : Controller
    {
        //
        // GET: /Store/

        public ActionResult Index()
        {
            var genre = new List<Genre>
            {
                new Genre{ Name = "Disco"},
                new Genre{ Name = "Jazz"},
                new Genre{ Name = "Rock"}
            };
            return View();
        }

............

Genre Class

namespace MvcMusicStore.Models
{
    public class Genre
    {
        public string Name 开发者_如何学运维{get; set;}

    }
}

Album Class

namespace MvcMusicStore.Models
{
    public class Album
    {
        public string Title { get; set; }
        public Genre Genre { get; set; }
    }
}

Index.cshtml

@model IEnumerable<MvcMusicStore.Models.Genre>

@{
    ViewBag.Title = "Store";
}

<h2>Browse Genre</h2>

<p>Select from @Model.Count() genres:</p>
<ul>
    @foreach (var genre in Model)
    {
        <li>@genre.Name</li>
    }
</ul>

Error I am getting is this

Error while running Mvc Music store


The reason this happen sis because you haven't passed any model to the view. So in the Index action instead of:

return View();

do:

return View(genre);

You have defined the genre list variable but you need to pass it to the view.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜