标题差不多说明了一切。我有一个复杂的EF4实体对象,它有一个小对象列表,我想在我的视图中绑定到复选框。我只是不知道如何满足html.checkboxfor()的第一个参数。IntelliSense总是给我一个错误。
以下是我的视图模型:
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; }
}
以及我的(很可能很糟糕)控制器代码,它填充了admingamereviewModel并将其发送到视图:
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>