使用viewmodel

时间:2016-05-23 08:38:54

标签: c# asp.net-mvc

我有以下控制器:

 public class GamesController : Controller
    {
        private AppDBContext db = new AppDBContext();
        private ConsoleBuilder cb = new ConsoleBuilder();

        public ActionResult Create()
        {
            var gc = new GameCreation();
            gc.Consoles = cb.BuildList(db);       
            return View(gc);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "ID,Title,ReleaseDate,Genre,Console")] Game game)
        {
            if (ModelState.IsValid)
            {
                db.Games.Add(game);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(game);
        }

创建视图的相关部分:

@model MyOwnWebsiteASP4dot6.ViewModels.GameCreation

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Game</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">

        <div class="form-group">
            @Html.LabelFor(model => model.game.Genre, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.game.Genre, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.game.Genre, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.game.Console, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
              @*@Html.EditorFor(model => model.Console.Title, new { htmlAttributes = new { @class = "form-control" } })*@
                @Html.DropDownListFor(model => model.game.Console, Model.Consoles)
                @Html.ValidationMessageFor(model => model.game.Console.Title, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

现在我在[HttpPost] Create函数的开头放了一个断点。此时我收到所有数据,但Console为null。 (请注意,cb.BuildList返回一个为测试它而构建的简单选择列表。列表正确加载到视图中。)

为什么这不起作用?

类:

public class Game
    {
        public int ID { get; set; }
        public string Title { get; set; }
        public DateTime ReleaseDate { get; set; }
        public string Genre { get; set; }
        public Console Console { get; set; }
    }

public class Console
    {
        public int ID { get; set; }
        public string Title { get; set; }
        public DateTime ReleaseDate { get; set; }
        public string Company { get; set; }
    }

public class GameCreation
    {
        public Game game { get; set; }
        public SelectList Consoles { get; set; }
    }

GameCreation类的唯一原因是我想在我的视图中访问控制台,以便我可以为用户创建一个下拉列表来选择其中一个控制台

1 个答案:

答案 0 :(得分:0)

将创建帖子方法中的模型更改为GameCreation。像下面的代码一样

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "ID,Title,ReleaseDate,Genre,Console")] GameCreation game)
        {
            if (ModelState.IsValid)
            {
                db.Games.Add(game);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(game);
        }

因为您正在从create get方法发送GameCreation模型,而在视图页面中您正在使用相同的模型。但在post方法中,您使用的是不同的模型Game。这就是问题所在。