【问题标题】:ASP.NET MVC 2 - Using EF4 with List scaffoldingASP.NET MVC 2 - 使用带有列表脚手架的 EF4
【发布时间】: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


    【解决方案1】:

    我建议您使用视图模型并包含视图所需的属性:

    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,你不会后悔的。

    【讨论】:

    • 谢谢。我不确定视图模型是否适合。在查看您的代码时,这似乎是一个显而易见的选择。
    猜你喜欢
    • 2011-03-22
    • 2015-04-09
    • 2011-08-07
    • 2011-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-06
    • 1970-01-01
    相关资源
    最近更新 更多