难以弄清楚Html.CheckboxFor()想要什么作为LINQ表达式

时间:2010-11-02 22:46:13

标签: asp.net-mvc-2 checkbox entity-framework-4 views html-helper

标题就是这么说。我有一个复杂的EF4实体对象,它有一个较小的对象列表,我想绑定到我视图中的复选框。我只是无法弄清楚如何满足Html.CheckboxFor()的第一个参数。智能感知不断给我一个错误。

这是我的观点模型:

public class PlatformListing
{
    public Platform Platform { get; set; }
    public bool IsSelected { get; set; }
}

public class AdminGameReviewViewModel
{
    public Game GameData { get; set; }
    public List<Genre> AllGenres { get; set; }
    public List<PlatformListing> AllPlatforms { get; set; }
}

我的(很可能是可怕的)控制器代码填充AdminGameReviewViewModel并将其发送到视图:

    public ActionResult EditReview(int id)
    {
        var game = _siteDB.Games.Include("Genre").Include("Platforms").Include("Content").Single(g => g.GameID == id);
        var genres = _siteDB.Genres.OrderBy(g => g.Name).ToList();
        var platforms = _siteDB.Platforms.OrderBy(p => p.Name).ToList();
        List<PlatformListing> platformListings = new List<PlatformListing>();

        foreach (Platform platform in platforms)
        {
            platformListings.Add(new PlatformListing { Platform = platform, IsSelected = game.Platforms.Any(p => p.PlatformID == platform.PlatformID) ? true : false });
        }

        var model = new AdminGameReviewViewModel { GameData = game, AllGenres = genres, AllPlatforms = platforms }; 

        return View(model);
    }

我只是不确定我错过了什么,这让我疯狂。我发现的文档也没有真正阐明它。

编辑:相关的视图代码(用于创建和编辑的局部视图) -

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<HandiGamer.ViewModels.AdminGameReviewViewModel>" %>

<p>
    <%: Html.Label("Game Title") %>
    <%: Html.TextBoxFor(model => model.GameData.GameTitle) %>
    <%: Html.ValidationMessageFor(model => model.GameData.GameTitle) %>
</p>
<p>
    <%: Html.LabelFor(model => model.GameData.Genre) %>
    <%: Html.DropDownList("Genre", new SelectList(Model.AllGenres, "GenreID", "Name", Model.GameData.GenreID)) %>
</p>
<p>
    <%: Html.Label("Platforms") %><br />
    <% foreach(var item in Model.AllPlatforms) { %>
        <%: item.Platform.Name %> <%: Html.CheckBox("Platforms[]", item.IsSelected, new { id = item.Platform.PlatformID }) %><br />
    <% } %>
</p>
<p>
    <%: Html.Label("Review Title") %>
    <%: Html.TextBoxFor(model => model.GameData.Content.Title) %>
</p>
<p>
    <%: Html.Label("Review") %>
    <%: Html.TextAreaFor(model => model.GameData.Content.Text) %>
</p>
<p>
    <%: Html.Label("Review Score") %>
    <%: Html.DropDownList("Score", new SelectList(new int[] {1, 2, 3, 4, 5}, "ReviewScore")) %>
</p>
<p>
    <%: Html.LabelFor(model => model.GameData.Pros) %><br />
    <%: Html.TextBox("Pros[]") %><br />
    <%: Html.TextBox("Pros[]") %><br />
    <%: Html.TextBox("Pros[]") %><br />
    <%: Html.TextBox("Pros[]") %><br />
    <%: Html.TextBox("Pros[]") %>
</p>

1 个答案:

答案 0 :(得分:3)

我相信你想要的东西:

Html.CheckBoxFor(model => model.AdminGameReviewViewModel[i].IsSelected)

在视图中定义i的某个循环中。发布您的视图可能有助于更清楚。