2012-04-13 39 views
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 
+0

あなたはどのようなモデルを表示していますか? – MikeSW

+0

あなたの 'foreach'の前に' @if(Model.Performances) 'のようなものを追加してコレクションがヌルかどうかを確認します – MilkyWayJoe

答えて

1

あなたはリストを持たず、要素は1つしかありません。 foreachループを取り出します。

+0

私はもともとリストを持っていましたが、 1つの項目をテストする – user1300580

+0

@ user1300580 - より詳細なビューモデルを作成してみてください。エンティティモデルはビューモデルとしてはうまく機能しません。 –

+0

どうすればいいですか? – user1300580

0

コントローラでジャンルを制作者に変更しました

関連する問題