Ajax.BeginForm绑定

时间:2015-09-16 11:10:20

标签: asp.net-mvc ajax.beginform

我的ajax表单绑定有一个小问题。这是一些代码:

我的编辑操作:

[HttpPost]
public ActionResult EditProductionArticle(Article article)
{
    if (ModelState.IsValid)
    {
        _ArticleRepository.Edit(article);
        return RedirectToAction("SelectArticle", new { id = article.DrawingNumber });
    }
    return PartialView(viewModel);

}

这是我的观点:

@using (Ajax.BeginForm("EditProductionArticle", new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = "selectPartialArticle" }))
{
    <form class="nonActive disabled" id="form">
        @Html.ValidationSummary(true)
        @Html.HiddenFor(model => model.productionArticle.DrawingNumber)

        @Html.LabelFor(p => p.productionArticle.ArticleDescr, "Article Description")
        @Html.TextBoxFor(p => p.productionArticle.ArticleDescr, new { @class = "form-control", @placeholder = "Article Description" })
        @Html.ValidationMessageFor(p => p.productionArticle.ArticleDescr)

        @Html.LabelFor(p => p.sellTray, "Trays")
        @Html.TextBoxFor(p => p.productionArticle.SellTrayCode, new { @class = "form-control", @placeholder = "Trays" })
        @Html.ValidationMessageFor(p => p.sellTray)

        @Html.LabelFor(p => p.productionArticle.BotCode, "Botanical Code")
        @Html.DropDownList("BotCode", null, new { @class = "form-control", @placeholder = "Botanical Code" })
        @Html.ValidationMessageFor(model => model.productionArticle.BotCode, "", new { @class = "text-danger" })

        @Html.LabelFor(p => p.productionArticle.CostGrpHL, "Cost Group HL")
        @Html.DropDownList("CostGrpHL", null, new { @class = "form-control", @placeholder = "Cost Group HL" })
        @Html.ValidationMessageFor(model => model.productionArticle.CostGrpHL, "", new { @class = "text-danger" })

        <input type="submit" value="EditProductionArticle" />
    </form>
}

我的问题如下:如果我将编辑操作修改为如下所示:

public ActionResult EditProductionArticle([Bind(Prefix = "productionArticle")]  Article article)
{ 
    //bla bla
}

除了下拉列表外,它还会绑定所有内容。如果我不这样做,它将绑定下拉列表,但没有别的。谁能想到办法做到这一点?

THX。

1 个答案:

答案 0 :(得分:2)

您使用@Html.DropDownList("BotCode", null, ...)表示您创建的下拉列表绑定到名为BotCode的属性,该属性在您的模型中不存在。您尚未向模型显示模型,但根据关联的LabelFor()ValidationMessageFor(),它包含名为productionArticle的属性,该属性是一个复杂对象,其属性名为BotCode

假设您在GET方法中将SelectLists分配给ViewBag属性,请为它们提供与您绑定的属性不同的名称,例如

ViewBag.BotCodeList = new SelectList(...) // or new List<SelectListItem>...

然后在视图中使用强类型帮助器绑定到模型属性

@Html.DropDownListFor(p => p.productionArticle.BotCode, (SelectList)ViewBag.BotCodeList, ...)

...(IEnumerable<SelectListItem>)ViewBag.BotCodeList