ASP.NET MVC2 和 EF4 - 在编辑视图中创建选中的复选框

发布于 2024-09-29 01:34:27 字数 1733 浏览 0 评论 0原文

我目前正在为我的游戏评论项目创建共享编辑/创建视图,但遇到了障碍。每个游戏都可以成为各种平台上的标题。我在 EF4 模型中将其映射为多对多关系。在我看来,我希望有一系列带有每个平台名称的复选框,并且对于“编辑”视图,选中正确的框。

我可以使用 HTML 帮助器轻松创建复选框。我最大的问题是弄清楚如何告诉助手在正确的平台值上打开 isChecked 。这是我到目前为止所拥有的:

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

<p>
    <%: Html.LabelFor(model => model.GameData.GameTitle) %>
    <%: Html.TextBoxFor(model => model.GameData.GameTitle) %>
    <%: Html.ValidationMessageFor(model => model.GameData.GameTitle) %>
</p>
<p>
    <%: Html.LabelFor(model => model.Genres) %>
    <%: Html.DropDownList("Genre", new SelectList(ViewData["Genres"] as IEnumerable, "GenreID", "Name", Model.GameData.GenreID) %>
</p>
<p>
    <%: Html.LabelFor(model => model.Platforms) %>
    <% foreach(var item in Model.Platforms) { %>
        <%: Html.CheckBox(item.Name) %>
    <% } %>
</p>

而且,我的视图模型是:

public class AdminGameReviewViewModel
{
    public Game GameData { get; set; }
    public List<Genre> Genres { get; set; }
    public List<Platform> Platforms { get; set; }
}

填充者:

    public ActionResult EditReview(int id)
    {
        var game = _siteDB.Games.Include("Genre").Include("Platforms").Include("Content").Single(g => g.GameID == id);
        var genres = _siteDB.Genres.ToList();
        var platforms = _siteDB.Platforms.ToList();
        var model = new { GameData = game, Genres = genres, Platforms = platforms }; 

        return View(model);
    }

所以,我真的只需要用逻辑推动正确的方向,以确定应该检查哪些框。

谢谢。

I'm currently in the process of creating a shared Edit/Create view for my game review project and have hit a snag. Each game can be a title on a variety of platforms. I have this mapped as a many-to-many relationship in my EF4 model. For my view, I'd like to have a series of checkboxes with the names of each platform, and, for the Edit view, have the correct boxes checked.

I can create the checkboxes easily with an HTML helper. My biggest problem is figuring out how to tell the helper to turn on isChecked on the correct platform values. Here's what I have so far:

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

<p>
    <%: Html.LabelFor(model => model.GameData.GameTitle) %>
    <%: Html.TextBoxFor(model => model.GameData.GameTitle) %>
    <%: Html.ValidationMessageFor(model => model.GameData.GameTitle) %>
</p>
<p>
    <%: Html.LabelFor(model => model.Genres) %>
    <%: Html.DropDownList("Genre", new SelectList(ViewData["Genres"] as IEnumerable, "GenreID", "Name", Model.GameData.GenreID) %>
</p>
<p>
    <%: Html.LabelFor(model => model.Platforms) %>
    <% foreach(var item in Model.Platforms) { %>
        <%: Html.CheckBox(item.Name) %>
    <% } %>
</p>

And, my view model is:

public class AdminGameReviewViewModel
{
    public Game GameData { get; set; }
    public List<Genre> Genres { get; set; }
    public List<Platform> Platforms { get; set; }
}

Populated by:

    public ActionResult EditReview(int id)
    {
        var game = _siteDB.Games.Include("Genre").Include("Platforms").Include("Content").Single(g => g.GameID == id);
        var genres = _siteDB.Genres.ToList();
        var platforms = _siteDB.Platforms.ToList();
        var model = new { GameData = game, Genres = genres, Platforms = platforms }; 

        return View(model);
    }

So, I really just need a nudge in the right direction with the logic to determine which boxes should be checked.

Thanks.

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

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

发布评论

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

评论(1

无法回应 2024-10-06 01:34:27

尝试使用 Html.CheckBoxFor

谓词是绑定到字段的表达式(应该是布尔值)。

<%: Html.CheckBoxFor(item => item.Name) %>

另外,我注意到在您的控制器中您返回了一个匿名类型 - 应该对您的 AdminGameReviewViewModel 视图模型进行强类型化,否则模型绑定将不起作用。

例如:

var model = new AdminGameReviewViewModel { GameData = game, Genres = genres, Platforms = platforms }; 

我不太确定您想要复选框的模型属性。您确定某个项目可以/不能有名称吗?听起来 item.Name 是一个字符串,对于复选框无效 - 您应该绑定到布尔标志。

也许您应该向 ViewModel 添加另一个属性?

Try using Html.CheckBoxFor

The predicate is an expression bound to the field (which should be a boolean).

<%: Html.CheckBoxFor(item => item.Name) %>

Also, i noticed in your Controller you are returning an anonymous type - that should be strongly-typed to your AdminGameReviewViewModel view-model, otherwise the model binding will not work.

e.g:

var model = new AdminGameReviewViewModel { GameData = game, Genres = genres, Platforms = platforms }; 

I'm not really sure what model property you want you want for the checkbox. Are you sure an item can/can't have a Name? It sounds like item.Name is a string, which is not valid for a checkbox - you should be binding to a boolean flag.

Maybe you should add another property to your ViewModel?

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