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>
谢谢。我不确定View Model是否可行。在看到你的代码时,它似乎是一个明显的选择。 – 2010-10-22 12:04:23