【发布时间】:2010-10-22 14:57:47
【问题描述】:
我想显示一个游戏评论列表以供编辑。使用 VS 的内置脚手架很容易做到这一点,但我在处理实体的关联 EntityCollections 时遇到了问题。我的控制器方法目前是:
public ActionResult ShowReviews()
{
var reviews = _siteDB.Games.Include("Genre").Include("Platforms").Include("Content").ToList();
return View(reviews);
}
如您所见,Platforms 是复数形式。每个游戏都可以在多种平台(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>
【问题讨论】:
标签: asp.net-mvc-2 views entity-framework-4