MVC2模型绑定瓦特/多到多方面的:处在风口浪尖 [英] MVC2 model binding w/ many-to-many: at the cusp

查看:89
本文介绍了MVC2模型绑定瓦特/多到多方面的:处在风口浪尖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一英寸距离让我创建的形式工作。我的问题是,一些数据传递到视图中模型的形式不被送回的回发

I'm an inch away from getting my Create form working. My problem is that some data passed into the form in the view model isn't being sent back in the postback.

模型:

Game:
    GameID - int (primary key, auto-incr)
    GameTitle - nvarchar(100)
    ReviewText - text
    ReviewScore - smallint
    Pros - text
    Cons - text
    LastModified - Datetime
    GenreID - int (foreign key from Genres)

Platform:
    PlatformID - int (primary key, auto-incr)
    Name - nvarchar(50)

GamePlatform (not visible as an EF 4 entity):
    GameID - int (foreign key from Games)
    PlatformID - int (foreign key from Platforms)

我使用的视图模型:

The view models I'm using:

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

和窗体本身:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<HandiGamer.WebUI.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.GenreID) %>
    <%: Html.DropDownListFor(m => Model.GameData.GenreID, new SelectList(Model.AllGenres, "GenreID", "Name", Model.GameData.GenreID)) %>
</p>
<p>
    <%: Html.Label("Platforms") %><br />
    <% for(var i = 0; i < Model.AllPlatforms.Count; ++i)
       { %>
           <%: Model.AllPlatforms[i].Platform.Name %> <%: Html.CheckBoxFor(p => Model.AllPlatforms[i].IsSelected) %>
    <% } %>
</p>
<p>
    <%: Html.Label("Review") %>
    <%: Html.TextAreaFor(model => Model.GameData.ReviewText) %>
</p>
<p>
    <%: Html.Label("Review Score") %>
    <%: Html.DropDownListFor(m => Model.GameData.ReviewScore,  new SelectList(new int[] {1, 2, 3, 4, 5}))%>
</p>
<p>
    <%: Html.LabelFor(model => model.GameData.Pros) %><br />
    <%: Html.TextAreaFor(model => model.GameData.Pros) %>
</p>
<p>
    <%: Html.LabelFor(model => model.GameData.Cons) %><br />
    <%: Html.TextAreaFor(model => model.GameData.Cons) %>
</p>

最后,我的创建方法(如准系统我试图得到这个工作):

And, finally, my Create method (barebones as I'm trying to get this to work):

    [HttpPost]
    public ActionResult CreateReview([Bind(Prefix = "GameData")]Game newGame, [Bind(Prefix = "AllPlatforms")]List<PlatformListing> PlatformList)
    {
        try
        {
            foreach(var plat in PlatformList)
            {
                if (plat.IsSelected == true)
                {
                    newGame.Platforms.Add(plat.Platform);
                }
            }

            newGame.LastModified = DateTime.Now;

            _siteDB.Games.AddObject(newGame);
            _siteDB.SaveChanges();

            // add form data to entities, then save
            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

问题是,当然,我的复选框。布尔isSelected被设定罚款,但数据被发布时,对应平台对象丢失。我以为他们会被自动传回。因此,在任何建议,如何传回一个PlatformListing的实际平台的一部分?

The problem is, of course, with my checkboxes. The boolean isSelected is being set fine, but when the data is being posted, the corresponding Platform object is missing. I thought they would automatically be passed back. So, any suggestions on how to pass back the actual Platform part of a PlatformListing?

推荐答案

对于获得的复选框值,如何使用策略<一个href=\"http://stackoverflow.com/questions/220020/how-to-handle-checkboxes-in-asp-net-mvc-forms/220073#220073\">described这里?

Regarding getting the checkbox values, how about using the strategy described here?

关于绕过实体对象作为参数,我说你让你的数据层搞混到您的视图层,你应该使用专用的视图模型。

Regarding passing around entity objects as arguments, I'd say you're letting your data layer get mixed into your view layer, and you should use a dedicated ViewModel.

这篇关于MVC2模型绑定瓦特/多到多方面的:处在风口浪尖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆