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