2010-10-21 107 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

相关问题