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

发布于 2024-09-30 04:58:03 字数 3108 浏览 4 评论 0原文

标题已经说明了一切。我有一个复杂的 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>

The title just about says it all. I have a complex EF4 entity object that has a list of smaller objects I'd like to bind to checkboxes within my view. I just can't figure out how to satisfy the first argument of Html.CheckboxFor(). Intellisense keeps giving me an error.

Here's my view models:

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; }
}

And my (most likely horrible) controller code which populates the AdminGameReviewViewModel and sends it to the view:

    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);
    }

I'm just not sure what I'm missing, and it's driving me nuts. The documentation I've found hasn't really shed any light on it, either.

EDIT: relevant view code (partial view to be used for both Create and Edit) -

<%@ 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼前雾蒙蒙 2024-10-07 04:58:03

我相信您想要类似的东西:

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

在视图中定义的 i 的某个循环中。发布您的观点可能有助于使这一点更加清晰。

I believe you want something like:

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

within some loop that's defined i in your View. Posting your View might help make this clearer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文