Inject the returned HTML into a DOM element specified via UpdateTargetId
Controller Code
public ActionResult SubmitForm(MyViewModel model)
{
if (ModelState.IsValid)
{
// Perform save or processing logic
// Return a partial view to update part of the page
return PartialView("_SuccessMessage", model);
}
// If validation fails, return a partial with errors or re-render the form
return PartialView("_FormPartial", model);
}
Notes:
_SuccessMessage.cshtml and _FormPartial.cshtml are partial views (no layout).
The return type is ActionResult, returning PartialView(...).
Client-Side View Code (Razor)
@model MyViewModel
<div id="formContainer">
@using (Ajax.BeginForm("SubmitForm", "Home", new AjaxOptions
{
HttpMethod = "POST",
UpdateTargetId = "formContainer", // Where to inject the result
InsertionMode = InsertionMode.Replace,
OnSuccess = "onSuccess",
OnFailure = "onFailure"
}))
{
@Html.EditorFor(m => m.Name)
<input type="submit" value="Submit" />
}
</div>
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Returning a partial view from a controller and injecting it into a specific section of the page using an Ajax.BeginForm is a common pattern in ASP.NET MVC for dynamic content updates without reloading the whole page.
Ajax.BeginForm)PartialViewfrom the controllerUpdateTargetIdController Code
Notes:
_SuccessMessage.cshtmland_FormPartial.cshtmlare partial views (no layout).ActionResult, returningPartialView(...).Client-Side View Code (Razor)
Required Scripts:
Partial View Example:
_SuccessMessage.cshtmlOptional JavaScript Callbacks
End-to-End Flow
id="formContainer"viaUpdateTargetIdTips & Gotchas
jquery.unobtrusive-ajax.jsis loaded after jQuery_Layout.cshtml)UpdateTargetIdmust match the DOM element where you want to inject