2014-01-06 62 views
6

我想在一個視圖中使用兩個模型,但是從我理解我的程序只是看不到模型內的任何對象。如何在一個視圖中使用兩個IENUMrable模型

這是我的代碼。

型號:

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: The " System.Collections.Generic.IEnumerable 'does not contain a definition of" PostContent "not found method of expanding" PostContent ", which takes a first argument of type' System.Collections.Generic.IEnumerable "

我如何使它按預期工作?在互聯網上有很多像我這樣的問題,但我找不到與我的情況相符的問題。

+0

需要迭代它們在\的foreach –

回答

6

循環模型在MVC中可能有點令人困惑,僅僅是因爲模板助手(即, Html.DisplayForHtml.EditorFor)可以提供模板,幫助者將自動調用集合中的每個元素。這意味着,如果你是新來的MVC,和你沒有意識到一個DisplayTemplateEditorTemplate尚未規定的收集已經,它看起來好像簡單:

@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 
} 

所以請使用你喜歡的任何一個。但是,在某些時候,查看提供templates for your types是個好主意。 (注意:雖然本文是爲MVC 2編寫的,但建議仍然適用。)它們允許您從視圖中刪除循環邏輯,使其更清晰。當與Html.DisplayForHtml.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; } 
} 
+1

非常感謝,我用foreach循環去了。我不知道我的問題的解決方案將如此簡單! – user3163730

+0

@ user3163730不客氣。 :) –

0

您需要遍歷他們是這樣的:

@model MvcMusicStore.Models.ModelMix 

<h2>Index</h2> 

@for(var i=0; i<model.PostsObject.Count(); i++) 
{ 
    @Html.DisplayNameFor(model => model.PostsObject[i].PostContent) 
} 

而且還最好是保存的IList,而不是IEnumerable的,因爲這將有Count屬性,而不是使用COUNT()方法

+0

DisplayNameFor將只顯示標題,而不是實際的數據。這些通常用於表格的標題。 –

+0

是的,我知道。但原來的問題是關於DisplayNameFor方法,所以用它做了樣本 –

+0

聽起來對我很好! –

-1

通常你如果您傳遞任何類型的IEnumerable<>,則需要遍歷每個項目。由於您構建了一個半複雜模型,因此您需要在每個列表中顯示foreach項目。下面是從ASP.NET MVC的教程,我認爲會幫助你有點基礎的例子:

@model IEnumerable<ContosoUniversity.Models.Course> 

@{ 
    ViewBag.Title = "Courses"; 
} 

<h2>Courses</h2> 

<p> 
    @Html.ActionLink("Create New", "Create") 
</p> 
<table class="table"> 
    <tr> 
     <th> 
      @Html.DisplayNameFor(model => model.CourseID) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Title) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Credits) 
     </th> 
     <th> 
      Department 
     </th> 
     <th></th> 
    </tr> 

@foreach (var item in Model) { 
    <tr> 
     <td> 
      @Html.DisplayFor(modelItem => item.CourseID) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Title) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Credits) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Department.Name) 
     </td> 
     <td> 
      @Html.ActionLink("Edit", "Edit", new { id=item.CourseID }) | 
      @Html.ActionLink("Details", "Details", new { id=item.CourseID }) | 
      @Html.ActionLink("Delete", "Delete", new { id=item.CourseID }) 
     </td> 
    </tr> 
} 

</table> 

通常大多數人使用的ICollection對他們的名單,其中一個例子可能是在一個對象:

public virtual ICollection<Post> Posts { get; set; } 

來源: http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/reading-related-data-with-the-entity-framework-in-an-asp-net-mvc-application

我會建議從頭開始,因爲它會幫助你理解爲什麼你需要這樣做: http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc

+0

這適用於我只有一個模型時,我確實已經知道該如何工作(我認爲),我已經完成了該網站的教程(但對於mvc4--您製作電影數據庫的那個教程),但它們未包括在內在一個視圖中有兩個模型的任何示例。 – user3163730

+0

您可以簡單地從複雜對象中決定想要的模型。 I.E. Model.Object1.Whatever/Model.Object2.Whatever –

相關問題