1

我有一个创建 cshtml 页面,如下所示:-

@model MvcCommons.ViewModels.CompositeViewModel
@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>


@using (Html.BeginForm()) {
@Html.ValidationSummary(true)

<fieldset>
    <legend>Article</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.ArticleViewModel.Article.ArticleTitle)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.ArticleViewModel.Article.ArticleTitle)
        @Html.ValidationMessageFor(model => model.ArticleViewModel.Article.ArticleTitle)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.ArticleViewModel.Article.ArticleDate)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.ArticleViewModel.Article.ArticleDate)
        @Html.ValidationMessageFor(model => model.ArticleViewModel.Article.ArticleDate)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.ArticleViewModel.Article.ArticleText)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.ArticleViewModel.Article.ArticleText)
        @Html.ValidationMessageFor(model => model.ArticleViewModel.Article.ArticleText)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.ArticleViewModel.Article.ArticleSource)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.ArticleViewModel.Article.ArticleSource)
        @Html.ValidationMessageFor(model => model.ArticleViewModel.Article.ArticleSource)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.ArticleViewModel.Article.CategoryID, "Category")
    </div>
    <div class="editor-field">
        @Html.DropDownList("CategoryID", String.Empty)
        @Html.ValidationMessageFor(model => model.ArticleViewModel.Article.CategoryID)
    </div>

    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>
}

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

然后我在 Create Action 中获取 viewModel,如下所示:-

        [HttpPost]
    public ActionResult Create(CompositeViewModel viewModel)
    {
        if (ModelState.IsValid)
        {
            unitOfWork.ArticleRepository.Insert(viewModel.ArticleViewModel.Article);
            unitOfWork.Save();
            return RedirectToAction("Index");
        }

        PopulateDropDownList(viewModel.ArticleViewModel.Article.CategoryID);
        return View(viewModel);
    }

PopulateDropDownList 代码如下:-

        private void PopulateDropDownList(object selectedCategory = null)
    {
        var categoryQuery = unitOfWork.CategoryRepository.Get(
                        orderBy: q => q.OrderBy(d => d.CategoryTitle));
        ViewBag.CategoryID = new SelectList(unitOfWork.CategoryRepository.Get(), "CategoryID", "CategoryTitle", selectedCategory);

    }

我的问题是,无论我在下拉列表中选择什么值,CompositeViewModel viewModel 中的 CategoryID 始终为 0!在 Create cshtml 中输入的其他数据在 viewModel 中正确填充。

我无法发现我做错了什么。

感谢您的帮助和时间

4

2 回答 2

2

你不应该使用DropDownListFor

@Html.DropDownListFor(model => model.ArticleViewModel.Article.CategoryID, (SelectList)ViewBag.CategoryID) 
于 2012-08-23T08:01:39.197 回答
1

你应该写

@Html.DropDownList("ArticleViewModel.Article.CategoryID", ViewBag.CategoryID, null)

所以 ModelBinding 可以正常工作,甚至可以使用DropDownListFor<>().

于 2012-08-23T08:00:25.090 回答