0

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?

Jack
  • 319
  • 6
  • 16
  • possible duplicate of [MVC3 Unobtrusive Validation Not Working after Ajax Call](http://stackoverflow.com/questions/7048726/mvc3-unobtrusive-validation-not-working-after-ajax-call) – StriplingWarrior Nov 20 '14 at 16:47
  • do you load the – ilans Nov 20 '14 at 16:50
  • @ilanS if I call them on Layout only then they fire multiple times, causing several orders to be added – Jack Nov 20 '14 at 16:57
  • What does `RefreshCRUD` js function do? there's a chance the problem lies in there. – ilans Nov 20 '14 at 17:34
  • @ilanS it just refreshes the other partials so that they all show the same information – Jack Nov 21 '14 at 10:15

0 Answers0