无法获取@ Html.DropDownList的值

时间:2012-08-23 07:53:38

标签: c# asp.net-mvc asp.net-mvc-3 razor

我有一个Create cshtml Page,如下所示: -

@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中正确填充。

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

感谢您的帮助和时间

2 个答案:

答案 0 :(得分:2)

你不应该使用DropDownListFor

@Html.DropDownListFor(model => model.ArticleViewModel.Article.CategoryID, (SelectList)ViewBag.CategoryID) 

答案 1 :(得分:1)

你应该写

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

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