Problem:
I am trying to take advantage of the AjaxExtension BeginForm in order to submit form data. The issue I am noticing is that complex objects in my model, and all of their properties, are being lost on the AJAX post due to the way MVC handles model binding. I can't seem to find a way of getting around this while keeping the separation I need between the various views. I did take a look at Passing two models to Controller using Ajax BeginForm() but the solution did not apply to my question, as the views were not split up into partials relying on different models.
This is what my code looks like (the essence of it, at least - changed names):
A.cshtml
@model A
<div>
@using (Ajax.BeginForm("ActionA", "ControllerA", new { id = Model.Id }, new AjaxOptions { HttpMethod = "POST" }, new { id = "formId" }))
{
@Html.Partial("ModelForm", Model.B)
}
</div>
B.cshtml
@model B
...
<div>
<p>@Html.TextBoxFor(x => x.Prop1)</p>
</div>
<div>
@Html.EditorFor(x => x.ComplexObject1)
</div>
<div class="spacing">
<p>@Html.TextBoxFor(x => x.Prop2)</p>
</div>
<div class="spacing">
@Html.EditorFor(x => x.ComplexObject2)
</div>
...
A.cs
...
public B B { get; set;}
...
ControllerA.cs
...
public ActionResult ActionA(A model) {
//do something with model
}
...
When I put a breakpoint in the ActionResult in the controller, the model object is instantiated, but the B property is null. If I add an @Html.HiddenFor(x => x.B.Prop1) and run the same code, I see that B is not null, but the property I did assign for Prop1 is lost. Also, if I add Prop1 to A.cs and run the same code, I notice that what I expected as B's Prop1 is assigned to A's Prop1 (this helped me understand how the binding was working). I basically want the form to recognize that within B.cshtml, all of the Html helpers are filling out the B object, which is a property in the A object, which gets sent to my controller. Is this possible?
@Html.Partial("ModelForm", Model)or specify theHtmlFieldPrefixas per this answer so the controls are correctly named.