2010-10-21 44 views
0

我想顯示一個遊戲評論列表進行編輯。這很容易與VS的內置腳手架,但我遇到了處理實體關聯的EntityCollections的問題。我的控制器方法是:ASP.NET MVC 2 - 使用EF4與列表腳手架

public ActionResult ShowReviews() 
{ 
    var reviews = _siteDB.Games.Include("Genre").Include("Platforms").Include("Content").ToList(); 
    return View(reviews); 
} 

如您所見,平臺是複數。每款遊戲都可以在多種平臺上(PS3,XBox 360等)。

我想在我的列表中將平臺的名稱作爲CSV字符串。我只是不確定如何優雅地做到這一點,因爲我首先需要鑽取平臺,提取它們的名稱,並將它們追加到空字符串中。我只是覺得我正在以這種錯誤的方式去做,而在我看來,結果形成邏輯的引入讓我覺得是錯誤的。這是我現在的觀點。請讓我知道是否有更好的方法來做到這一點。

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<HandiGamer.Models.Game>>" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> 
ShowReviews 
</asp:Content> 

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 

    <h2>ShowReviews</h2> 

    <table> 
     <tr> 
      <th></th> 
      <th> 
       Game 
      </th> 
      <th> 
       Review Title 
      </th> 
      <th> 
       Genre 
      </th> 
      <th> 
       Platforms 
      </th> 
      <th> 
       Score 
      </th> 
      <th> 
       Last Modified 
      </th> 
     </tr> 

    <% foreach (var item in Model) { %> 
     <% var platformInfo = item.Platforms.ToList(); 
      string platformNameList = ""; 

      foreach (var platformName in platformInfo) { 
       platformNameList += platformName.Name + " "; 
      } 
     %> 

     <tr> 
      <td> 
       <%: Html.ActionLink("Edit", "Edit", new { id=item.GameID }) %> | 
       <%: Html.ActionLink("Details", "Details", new { id=item.GameID })%> 
      </td> 
      <td> 
       <%: item.GameTitle %> 
      </td> 
      <td> 
       <%: item.Genre.Name %> 
      </td> 
      <td> 
       <%: platformNameList %> 
      </td> 
      <td> 
       <%: item.ReviewScore %> 
      </td> 
      <td> 
       <%: item.Content.LastModified %> 
      </td> 
     </tr> 

    <% } %> 

    </table> 

    <p> 
     <%: Html.ActionLink("Create New", "CreateReview") %> 
    </p> 

</asp:Content> 

回答

2

我會建議你使用一個視圖模型,包括由視需要的屬性:

public class ReviewViewModel 
{ 
    public string Title { get; set; } 
    public string Genre { get; set; } 
    public string Platforms { get; set; } 
    ... 
} 

,並盡一切必要的投影在你的控制器:

public ActionResult ShowReviews() 
{ 
    var reviews = _siteDB.Games.Include("Genre").Include("Platforms").Include("Content").ToList(); 
    var model = reviews.Select(r => new { 
     Title = r.GameTitle, 
     Genre = r.Genre.Name, 
     Platforms = string.Join(" ", r.Platforms.Select(p => p.Name).ToArray()); 
    }); 
    return View(model); 
} 

現在你的看法將變得更清潔:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<HandiGamer.Models.ReviewViewModel>>" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> 
ShowReviews 
</asp:Content> 

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 

    <h2>ShowReviews</h2> 

    <table> 
     <thead> 
      <tr> 
       <th> 
        Title 
       </th> 
       <th> 
        Genre 
       </th> 
       <th> 
        Genre 
       </th> 
       <th> 
        Platforms 
       </th> 
      </tr> 
     </thead> 
     <tbody> 
      <%: Html.DisplayForModel() %> 
     </tbody> 
    </table> 

    <p> 
     <%: Html.ActionLink("Create New", "CreateReview") %> 
    </p> 

</asp:Content> 

和審查(~/Views/Home/DisplayTemplate/ReviewViewModel.ascx)小的顯示模板:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<HandiGamer.Models.ReviewViewModel>" %> 
<tr> 
    <td> 
     <%: Model.Title %> 
    </td> 
    <td> 
     <%: Model.Genre %> 
    </td> 
    <td> 
     <%: Model.Platforms %> 
    </td> 
</tr> 

最後看看MVCContrib Grid,你會不會後悔。

+0

謝謝。我不確定View Model是否可行。在看到你的代碼時,它似乎是一個明顯的選擇。 – 2010-10-22 12:04:23

相關問題