In my code, i use ajax.beginform for collection entries (with modal window for new entries) then refresh the list.
Controller
C#
public ActionResult BusinessCategoryNewUpdateRow(BusinessCategoryViewModel vmodel)
{
try
{
vmodel.Subcategories.Add(vmodel.NewEditEntry);
return PartialView("BusinessCategory/SubBusinessCategoryView", vmodel);
}
catch
{
return View();
}
}View :
TypeScript
@using (Ajax.BeginForm("BusinessCategoryNewUpdateRow", "BusinessReference", new AjaxOptions
{ UpdateTargetId = "subCategoryPart", OnSuccess = "clearDialog" }, new { id = "showWin" }))
{
@Html.LabelFor(model => model.MainCategory.name, new { @class = "floating-label" })
@Html.EditorFor(model => model.MainCategory.name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.MainCategory.name, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.MainCategory.business_cat_code, new { @class = "floating-label" })
@Html.EditorFor(model => model.MainCategory.business_cat_code, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.MainCategory.business_cat_code, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.MainCategory.description, new { @class = "floating-label" })
@Html.EditorFor(model => model.MainCategory.description, "Text", new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.MainCategory.description, "", new { @class = "text-danger" })
Sub-Category
@*Add*@
Add
@*popup window*@
Add/Edit Subcategory
×
@Html.LabelFor(model => model.NewEditEntry.name, new { @class = "floating-label" })
@Html.EditorFor(model => model.NewEditEntry.name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.NewEditEntry.name, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.NewEditEntry.business_cat_code, new { @class = "floating-label" })
@Html.EditorFor(model => model.NewEditEntry.business_cat_code, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.NewEditEntry.business_cat_code, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.NewEditEntry.description, new { @class = "floating-label" })
@Html.EditorFor(model => model.NewEditEntry.description, "Text", new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.NewEditEntry.description, "", new { @class = "text-danger" })
Save
Close
@*popup window*@
@Html.Partial("BusinessCategory/SubBusinessCategoryView", Model)
}This part is ok, the problem is when i include/add Html.BeginForm outside Ajax.BeginForm, the add button of modal window triggers the Html.BeginForm instead of the Ajax.BeginForm.
What is the best approach to handle collections (parent-child entries) then perform a final submit using Html.BeginForm and Ajax.BeginForm?
Is there a similar code snippet to cater parent-child entries?

Tuhin PaulRank #7 · 71.9K reputationPosted Nov 10, 2023, 4:03 AM EST
view code:
@using (Html.BeginForm("FinalSubmitAction", "ControllerName", FormMethod.Post, new { id = "parentForm" })) { @using (Ajax.BeginForm("BusinessCategoryNewUpdateRow", "BusinessReference", new AjaxOptions { UpdateTargetId = "subCategoryPart", OnSuccess = "clearDialog" }, new { id = "childForm" })) { Add Child Entry } @Html.Partial("BusinessCategory/SubBusinessCategoryView", Model) Final Submit }JS code:
$(document).ready(function () { $('#childForm').on('submit', function (e) { e.preventDefault(); // Prevent the default form submission behavior $.ajax({ url: $(this).attr('action'), type: $(this).attr('method'), data: $(this).serialize(), success: function (response) { $('#subCategoryPart').html(response); // Update the child entries clearDialog(); // Optionally, clear the modal dialog } }); }); });The JavaScript code handles the AJAX submission of child entries and updating the display of child entries without a full page reload.