This is a follow up to MVC3 form in partial view
Although the author gave a workaround to the problem. I would like to find out a proper answer why this is not working.
I don’t have access to original code, but here’s mine (so I can answer following questions):
// main view (which is partial too)
@foreach (AddingComponentVM sc in Model)
{
@Html.Partial("_SearchIngredientUpdate", sc);
}
//partial view
@using (Ajax.BeginForm("IngredientSearchUpdate", new { controller = "Recipe" }, ajxOpt, new { id = "addingWidgetForm" + Model.IngredientID }))
{
@Html.TextBoxFor(model => model.IngredientID)
@Model.IngredientID
}
@Model.IngredientID contains proper value. But the textbox contains value of the model sent to controller (sic!) and it is obviously the same for each form.
[AjaxOnly]
public JsonResult IngredientSearchUpdate(
AddingComponentVM dataIn,
[ModelBinder(typeof(SearchOptionsBinder))] SearchOptions sessionSO)
If action without AddingComponentVM in signature calls the same code above, forms renders correctly.
public PartialViewResult IngredientSearch([ModelBinder(typeof(SearchOptionsBinder))] SearchOptions sessionSO)
Anyone could point me out to the cause of this strange (at least for me) behaviour? Thanks!
I couldn’t sleep because of this, but here’s the answer:
It does not matter whether you use PartialView or EditorTemplates. As described here: How to modify posted form data within controller action before sending to view?
“HTML Helpers uses the following order precedence when attempting lookup of the key:
typed view. This property is a shortcut to View.ViewData.Model)
So if any values were posted it is enough to clear StateModel collection and then the data from model can be pick up by html helper used. This will do the trick: