Spluf
Spluf

Reputation: 820

Ajax.BeginForm Binding

I have a small problem with my ajax form binding. Here is some code:

my edit action:

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

}

and here is my view:

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

My problem is the following: If i modify my edit action to look like this:

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

it will bind everything but the dropdowns. If i don't, it will bind the dropdowns but nothing else. Can anyone think of a way to do this?

Thx.

Upvotes: 0

Views: 991

Answers (1)

user3559349
user3559349

Reputation:

You use of @Html.DropDownList("BotCode", null, ...) means you creating a dropdownlist that is binding to a property named BotCode which does not exist in your model. You have not shown you model but based on the associated LabelFor() and ValidationMessageFor() it contains a property named productionArticle which is a complex object with a property named BotCode

Assuming your assigning the SelectLists to ViewBag properties in your GET method, give them a different name from the property your binding to, say

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

and then in the view use the strongly typed helpers to bind to your model property

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

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

Upvotes: 2

Related Questions