开发者

Having difficulty figuring out what Html.CheckboxFor() wants as a LINQ expression

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; }
    publ开发者_如何转开发ic 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>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜