Asp.net-Mvc

如何在一個視圖中使用兩個 IENumerable 模型

  • January 6, 2014

我試圖在一個視圖中使用兩個模型,但據我了解,我的程序只是看不到模型中的任何對象。

這是我的程式碼。

楷模:

   public class Album
   {
       [Key]
       public int ThreadId { get; set; } 
       public int GenreId { get; set; }
       public string Title { get; set; }
       public string ThreadByUser { get; set; } 
       public string ThreadCreationDate { get; set; }
       public string ThreadContent { get; set; } 
       public Genre Genre { get; set; }
       public List<Posts> Posty { get; set; }
   }
   public class Posts 
   {
       [Key]
       public int PostId { get; set; }
       public int ThreadId { get; set; } 
       public string PostTitle { get; set; }
       public string PostContent { get; set; }
       public string PostDate { get; set; }
       public string PosterName { get; set; }
       public Album Album { get; set; }

   }

   public class ModelMix
   {
       public IEnumerable<Posts> PostsObject { get; set; }
       public IEnumerable<Album> ThreadsObject { get; set; }
   }  

索引控制器程式碼:

   public ActionResult Index(int id)
   {

       ViewBag.ThreadId = id;
       var posts = db.Posts.Include(p => p.Album).ToList();
       var albums = db.Albums.Include(a => a.Genre).ToList();

       var mixmodel = new ModelMix
       {
           PostsObject = posts,
           ThreadsObject = albums
       };

       return View(mixmodel);
   }

查看程式碼:

@model MvcMusicStore.Models.ModelMix

<h2>Index</h2>

@Html.DisplayNameFor(model => model.PostsObject.PostContent)

當我嘗試執行我的程序時,我收到了這個錯誤:

CS1061:“System.Collections.Generic.IEnumerable”不包含“PostContent”的定義,未找到擴展“PostContent”的方法,該方法採用“System.Collections.Generic.IEnumerable”類型的第一個參數

我怎樣才能讓它按預期工作?網際網路上有很多像我這樣的問題,但我找不到任何符合我的情況的問題。

在 MVC 中開始循環模型可能有點令人困惑,只是因為可以為模板化的幫助器(即Html.DisplayForHtml.EditorFor)提供模板,幫助器將為集合中的每個元素自動呼叫這些模板。這意味著,如果您是 MVC 新手,並且您沒有意識到尚未為集合提供aDisplayTemplate或 an ,那麼它看起來很簡單:EditorTemplate

@Html.DisplayFor(m => m.SomePropertyThatHoldsACollection)

是你所需要的全部。因此,如果您已經看到類似的東西,那可能就是您假設它會起作用的原因。但是,讓我們暫時假設沒有提供模板。你有兩個選擇。

首先,也是最簡單的,將foreach在集合上使用:

@foreach (var post in Model.PostsObject)
{
   @Html.DisplayFor(m => post.PostTitle)
   // display other properties
}

您也可以使用for循環,但使用IEnumerable<T>,沒有索引器,所以這不起作用:

@for (int i = 0; i < Model.PostsObject.Count(); i++)
{
   // This generates a compile-time error because
   // the index post[i] does not exist.
   // This syntax would work for a List<T> though.
   @Html.DisplayFor(m => post[i].PostTitle)
   // display other properties
}

如果你還想使用for循環,你可以像這樣使用它:

@for (int i = 0; i < Model.PostsObject.Count(); i++)
{
   // This works correctly
   @Html.DisplayFor(m => post.ElementAt(i).PostTitle)
   // display other properties
}

所以使用任何你喜歡的。但是,在某些時候,為您的類型提供模板是個好主意。(注意:儘管本文是為 MVC 2 編寫的,但建議仍然適用。)它們允許您從視圖中刪除循環邏輯,使它們更清晰。當與Html.DisplayFor, 或結合使用時Html.EditorFor,它們還將為模型綁定生成正確的元素命名(這很棒)。它們還允許您重用類型的表示。

我要說的最後一條評論是您的屬性的命名有點冗長:

public class ModelMix
{
   public IEnumerable<Posts> PostsObject { get; set; }
   public IEnumerable<Album> ThreadsObject { get; set; }
}

我們已經知道它們是對象,因此無需在最後添加。這更具可讀性:

public class ModelMix
{
   public IEnumerable<Posts> Posts { get; set; }
   public IEnumerable<Album> Threads { get; set; }
}

引用自:https://stackoverflow.com/questions/20953877