2013-07-06 86 views
4

試圖打印出每個電影項目的名稱。用剃刀循環瀏覽模型

例如。

  • 黑暗騎士

我缺少的剃刀代碼中的一些聲明語句?智能感知模型

[XmlRoot("movies")] 
     public class MovieSummary 
     { 
     [XmlElement("movie")] 
     public List<Movie> Movies { get; set; } 
     } 

     public class Movie 
     { 
     public int id { get; set; } 
     public string name { get; set; } 
     } 

     public ActionResult Index() 
     { 
     MovieSummary summary = Deserialize(); 
     return View(summary); 
     } 

     public static MovieSummary Deserialize() 
     { 
     using (TextReader reader = new StreamReader("c:\\movies.xml")) 
     { 
      XmlSerializer serializer = new XmlSerializer(typeof(MovieSummary)); 
      return (MovieSummary)serializer.Deserialize(reader); 
     } 
     } 

     <?xml version="1.0" ?> 
     <movies xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
     <movie> 
     <id>1</id> 
     <name>The Dark Knight</name> 
     </movie> 
     </movies> 

     <ul> 
     @if (Model != null) { 
      foreach (var movies in Model.Movies) 
      { 
       <li>@movies.movie.name</li> 
      } 
     } 
     </ul> 

回答

10

後沒有列出任何選擇在您查看文件的頂部,聲明你的模型類型,如下所示:

<ul> 
     @if (Model != null) 
     { 
      foreach (var movie in Model.Movies) 
      { 
       <li>@movie.name</li> 
      } 
     } 
</ul> 

@model MovieSummary 

然後在您的收藏Movies迭代