ASP.NET MVC 2 - 将 EF4 与列表脚手架结合使用
我想显示游戏评论列表以供编辑。使用 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>
I want to show a list of game reviews for editing. This is simple to do with VS' built-in scaffolding, but I'm running into issues with handling an entity's associated EntityCollections. My controller method is currently:
public ActionResult ShowReviews()
{
var reviews = _siteDB.Games.Include("Genre").Include("Platforms").Include("Content").ToList();
return View(reviews);
}
As you can see, Platforms is plural. Each game can be on a multitude of platforms (PS3, XBox 360, etc.).
I'd like to have the platforms' names as a CSV string in my list. I'm just not sure how to do it elegantly as I first need to drill into the platforms, extract their names, and append them to an empty string. I just feel like I'm going about this the wrong way, and the introduction of result shaping logic in my view strikes me as wrong. Here's my view as it stands now. Please let me know if there's a better way to do this.
<%@ 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您使用视图模型并包含视图所需的属性:
并在控制器中进行所有必要的投影:
现在您的视图将变得更加清晰:
以及用于审查的小显示模板(
~/ Views/Home/DisplayTemplate/ReviewViewModel.ascx
):最后看一下 MVCContrib Grid,您不会后悔的。
I would recommend you to use a view model and include properties needed by the view:
and do all the necessary projections in your controller:
And now your view will become much cleaner:
and a small display template for the review (
~/Views/Home/DisplayTemplate/ReviewViewModel.ascx
):Finally take a look at MVCContrib Grid and you won't regret it.