I am trying to post data from a partial view form (viewbag) to an action method on the main controller. I am using Jquery:
$.ajax({
url: '@Url.Action("displaypreviewformactionmethod", "MyMainController")',
type: 'post',
dataType: 'json',
data: $('form#realform').serialize(),
success: function (data) {
$('#DivTagWhereMyPartialViewIs').html(data);
$('#DivTagWhereMyPartialViewIs').show();
}
My Partial View with the Form looks like:
<div id="step-3" class="">
@using (Html.BeginForm("displaypreviewformactionmethod", "MyMainController"))
{
foreach (var frm in Model)
{
<tr>
<td>
@Html.LabelFor(model => model.FirstOrDefault().ELabel, frm.SomeText)
</td>
<td>
@Html.TextBox(frm.FieldName)<br />
</td>
</tr>
}
<input type="submit" name="Submit" value="Submit" />
}
</div>
The action method in the main controller is:
public ActionResult displaypreviewformactionmethod(FormCollection form)
{
if (form.Count > 0)
{
ViewBag.FormItems = form;
}
return PartialView("MyPartialView", form);
}
The action gets called, but it doesn't pass anything in the formcollection to the partial view- which leads me to believe that I'm not correctly identifying the form in the partial view. I tried adding <form id="realform">
before the in the partial view but that didn't work. Is there some other way I can identify the form in Jquery?
Any direction much appreciated.