My mark up has one div with id= "divGrid" and one ajax enabled form outside that div. Form contains two text inputs and one submit button with UpdateTargetId = "divGrid". It does update that grid perfectly but add duplicate ajax-enabled form on the page. Why this? How to fix this?
Here is my View:
@model List<EF.Models.Data.Books>
@{
ViewBag.Title = "CodeFirst";
Layout = null;
}
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
<div id="divGrid">
@foreach (var item in Model)
{
<table>
<tr>
<td>@item.ID</td>
<td>@item.Name</td>
<td>@item.Price</td>
</tr>
</table>
}
</div>
@using (Ajax.BeginForm(new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "divGrid", InsertionMode = InsertionMode.Replace }))
{
@Html.TextBox("name");
<br />
@Html.TextBox("price");
<input type="submit" value="Insert" />
}
My controller action:
[HttpPost]
public ViewResult CodeFirst(FormCollection form)
{
book.Name = form["name"];
book.Price = Convert.ToDecimal(form["price"]);
context.Books.Add(book);
context.SaveChanges();
result = context.Books.ToList();
return View(result);
}
Interestingly, my rendered mark-up is something like
<div id="divGrid">
<div id="divGrid">
<!-- Data display as expected -->
</div>
<!-- ajax enabled form, that is out side of this in source code -->
</div>
<!-- ajax enabled form, that is out side of this in source code -->
