So I have three partial views on one page that perform Create, Update and Delete functions.
<div id="create-example">
@Html.Action("OrderCreate","Home")
</div>
<div id="update-example">
@Html.Action("OrderUpdate","Home")
</div>
<div id="delete-example">
@Html.Action("OrderDelete","Home")
</div>
If I include the jquery.unobtrusive-ajax.js and jquery.validate.js in each of the partial views the AJAX form successfully works in all of them however because I refresh all 3 (onsuccess) the .js files are called multiple times causing multiple creates/updates/deletes.
Now I know you should only load the files once, but when I put them in my _Layout.cshtml file, they do not get loaded and when I submit one of the partial view forms it still works but doesn't stay on the same page and goes to the controller/action page.
Below is the Create partial view (which only works by including the js scripts directly in the AJAX form)
@model EntityFrameworkTutorial.Models.OrderViewModel
@{ var quantities = new SelectList(Enumerable.Range(1, 10)); }
@using (Ajax.BeginForm("OrderCreate", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "create-example", InsertionMode = InsertionMode.Replace, OnSuccess="RefreshCRUD" }))
{
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<br />
<label for="Order_OrderCustomerID" class="col-sm-2 control-label">Customer</label>
@Html.DropDownListFor(m => m.Order.OrderCustomerID, Model.CustomerList)
<br /><br />
<label for="Order_OrderProductID" class="col-sm-2 control-label">Product</label>
@Html.DropDownListFor(m => m.Order.OrderProductID, Model.ProductList)
<br /><br />
<label for="Order_OrderQuantity" class="col-sm-2 control-label">Quantity</label>
@Html.DropDownListFor(m => m.Order.OrderQuantity, quantities)
<br /><br />
<input type="submit" class="btn btn-default btn-lg" value="Create Order" />
}
-------
Just tried adding
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
in each of the partial views and it solved loading the .js files only once in _Layout however when I create an order, multiple orders still get created!!
Any ideas?