1

我正在使用一个类来存储数据,然后使用控制器和视图在使用 MVC3 的网站上的屏幕上显示数据,但是我遇到了一个错误,不胜感激。

班级:

public class SampleData : DropCreateDatabaseIfModelChanges<TicketBookingEntities>
    {
        protected override void Seed(TicketBookingEntities context)
        {
            var productions = new List<Production>
            {
                new Production { Name = "Peter Pan" },
                new Production { Name = "Mary Poppins" },
                new Production { Name = "Pirates of the Carribean" },
                new Production { Name = "Joseph" },
                new Production { Name = "Billy Elliot" },

            };

            var directors = new List<Director>
            {
                new Director { Name = "Jason Brown" },
                new Director { Name = "Dan Elish" },
                new Director { Name = "Lee Hall" },
                new Director { Name = "Billie Armstrong" },
                new Director { Name = "Willy Russell" },

            };

            new List<Performance>
            {


                new Performance {Title = "Test", Genre = productions.Single(g => g.Name == "Peter Pan"), Director = directors.Single(a => a.Name == "Jason Brown"), Price = 9.99M, AlbumArtUrl = "/Content/Images/placeholder.gif" },


            }.ForEach(a => context.Performances.Add(a));
        }
    }
}

控制器:

 public ActionResult Browse(string genre)
        {
            var productionModel = storeDB.Productions.Include("Performances")
                .Single(g => g.Name == genre);

            return View(productionModel);

        }

看法:

@model Assignment2.Models.Production

@{
    ViewBag.Title = "Browse";
}

<h2>Browsing Production: @Model.Name</h2>
<ul>
    @foreach (var performance in Model.Performances)
    {
        <li>
            @performance.Title

        </li>
    }

</ul>

错误:

Sequence contains no elements
4

2 回答 2

1

你没有一个列表,你只有一个元素。取出 foreach 循环。

于 2012-04-13T18:48:25.387 回答
0

设法解决了问题,将类型更改为控制器上的生产

于 2012-04-14T10:23:36.863 回答