Ajax.BeginForm only displaying partial View instead of parent & partial view on return

Question!

I have a parent View & a child View.

When posing with the Ajax.BeginForm, I'm expecting back the entire parent view plus the results of the partial view updated. Only the results of the partial view is displayed.

In addition, the "OnSuccess" method doesn't seem to be getting hit as I'm debugging.

Can someone please tell me what I'm doing incorrecty?

Controller:

public class HomeController : Controller
    {
        private DAL db = new DAL();

        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to YeagerTech!";

            return View();
        }

        // GET: Categories/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Categories/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Create(Category category)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    await db.AddCategoryAsync(category);
                }
                catch (System.Exception ex)
                {
                    throw ex;
                }
            }

            return View(category);
        }

        public PartialViewResult ShowDetails()
        {
            //string code = Request.Form["txtCode"];
            Category cat = new Category();
            //foreach (Product p in prodList)
            //{
            //    if (p.ProdCode == code)
            //    {
            //        prod = p;
            //        break;
            //    }
            //}
            cat.CategoryID = 1;
            cat.Description = "Financial";
            return PartialView("_ShowDetails", cat);
        }

    }

Parent View

@model Models.Models.Category

@{
    ViewBag.Title = "Create Category";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Create Category</h2>

@using (Ajax.BeginForm("ShowDetails", "Home", new AjaxOptions
{
    HttpMethod = "POST",
    UpdateTargetId = "div1",
    InsertionMode = InsertionMode.Replace,
    OnSuccess = "OnSuccess",
    OnFailure = "OnFailure"
}, new { @class = "form-horizontal" }))
{
    @Html.AntiForgeryToken()
        @Html.ValidationSummary()
        <div class="body-content">
            <h4>Category</h4>
            <hr />
            <div class="form-group">
                <div class="col-offset-1 col-lg-11 col-md-11 col-sm-11 col-xs-11">
                    @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control", @placeholder = "Description" } })
                    @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                <div class="col-lg-11 col-md-11 col-sm-11 col-xs-11">
                    <button type="submit" id="btnCategoryCreate" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span>Create</button>
                </div>
            </div>
        </div>
}

    <div id="div1">

    </div>

    @*<div>
        @Html.ActionLink("Back to List", "Index")
        @Html.Hidden("categoryCreateUrl", Url.Action("Create", "Home"))
    </div>*@

    @section Scripts {
        <script>
            $(document).ready(function ()
            {
                function OnSuccess(response)
                {
                    $('#form1').trigger("reset");
                }

                //if (typeof contentCreateCategory == "function")
                //    contentCreateCategory();
            });
        </script>
    }

Partial View

@model Models.Models.Category

    <h1>Cat Details</h1>
    <h2>
        Cat Code: @Model.CategoryID<br />
        Cat Name: @Model.Description<br />
    </h2>

EDIT # 1

Parent & child view displayed (due to a missing JS file, thanks to the mention of Stephen), plus was able to programmatically clear out the form with the OnSuccess method.

I had thought since that was JS, it needed to go in the Scripts section, but did not recognize it there.

Here is the finished code.

@using (Ajax.BeginForm("ShowDetails", "Home", new AjaxOptions
    {
        HttpMethod = "POST",
        UpdateTargetId = "div1",
        InsertionMode = InsertionMode.Replace,
        OnSuccess = "OnSuccess",
        OnFailure = "OnFailure"
    }, new { @id = "frm1", @class = "form-horizontal" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary()
    <div class="body-content">
        <h4>Category</h4>
        <hr />
        <div class="form-group">
            <div class="col-offset-1 col-lg-11 col-md-11 col-sm-11 col-xs-11">
                @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control", @placeholder = "Description" } })
                @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-lg-11 col-md-11 col-sm-11 col-xs-11">
                <button type="submit" id="btnCategoryCreate" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span>Create</button>
                <button type="reset" id="btnClear" class="btn btn-default"><span class="glyphicon glyphicon-eye-close"></span>Clear</button>
            </div>
        </div>
    </div>
}

<div id="div1">

</div>

<script type="text/javascript">

    function OnSuccess(response)
    {
        $('#frm1').trigger("reset");
    }

    function OnFailure(response)
    {
        alert("Whoops! That didn't go so well did it?");
    }

</script>
By : sagesky36


Answers

A bit late for answer, but yet it might help someone else.

The issue might be that the javascript files required for Ajax.BeginForm are not loaded in your page.

Microsoft.jQuery.Unobtrusive.Ajax

Nuget package to search for

By : cpoDesign


This video can help you solving your question :)
By: admin