难以弄清楚 Html.CheckboxFor() 想要什么作为 LINQ 表达式
标题已经说明了一切。我有一个复杂的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您想要类似的东西:
在视图中定义的
i
的某个循环中。发布您的观点可能有助于使这一点更加清晰。I believe you want something like:
within some loop that's defined
i
in your View. Posting your View might help make this clearer.