I am using MVC4. Unobtrusive JavaScript is enabled and referenced in my project. The Ajax scripts are loading in my shared master page. FireBug reports no errors or 404’s.
I have added a Text box to my page, which updates the contents of a Hidden field inside of my Ajax form. When a user presses a key, and the KeyUp event fires, I force my Ajax form to submit by calling:
$("form#ajaxForm").trigger("submit");
Where ajaxForm is the name of the Ajax form. This works fine. The form is submitted, behind the scenes, and my controller event fires. The Model is updated with the new data, based on the input from the user.
However, where it gets strange, is when the result returns to my form. The entire contents of my form div tag is replaced by the result — which is the entire page.
<div id="basic-modal-content"">
<input id="breed" type="text"/>
<div>
<form id="ajaxForm" method="post" data-ajax-update="#results" data-ajax-mode="replace" data-ajax="true" action="/Poultry/ListBreeds?Length=7">
<div id="results">
->THIS AREA IS GETTING REPLACED WITH THE ENTIRE FORM <-
<div id="basic-modal-content">
</div>
</div>
</form>
- Notice how basic-modal-content now contains itself, another basic-modal-content. The result div should only contain the updates results, not the entire view — obviously.
My code is as follows:
View (BreedSelectorPartial.cshtml)
@model IQueryable<Inert.BreedViewModel>
<script type="text/javascript">
$(document).ready(function () {
$("#breed").keyup(function (e) {
$("input[name=breedSearch]").val($("#breed").val());
$("form#ajaxForm").trigger("submit");
});
});
</script>
<div id="basic-modal-content">
<input id="breed" type="text" style="width:100%; height:22px;" />
<div>
<div style="margin-top: 4px;">
<label for="breed">Begin by typing a breed, e.g. <span style="font-style: italic">Wyandotte</span></label>
</div>
</div>
@using (Ajax.BeginForm("ListBreeds", "Poultry", new AjaxOptions {UpdateTargetId = "results" }, new { id = "ajaxForm" }))
{
<input id="breedSearch" name="breedSearch" type="hidden" />
}
<div id="results" style="height: 320px; color: black; margin-top: 12px; font-size: 18px;">
@{
var grid = new WebGrid(Model, ajaxUpdateContainerId: "results");
int c = Model.Count();
}
@grid.GetHtml(tableStyle: "webgrid",
alternatingRowStyle: "webgrid-alternating-row",
rowStyle: "webgrid-row-style",
displayHeader: true)
</div>
</div>
<script type="text/javascript" src="/scripts/jquery.simplemodal.js"></script>
<script type="text/javascript" src="/scripts/basic.js"></script>
Controller (PoultryController.cs)
[HttpPost]
public ActionResult ListBreeds(string breedSearch)
{
InertDatabaseEntitiesConnection db = new InertDatabaseEntitiesConnection();
var breeds = db.Breeds.Where(q => q.Name.Contains(breedSearch));
var names = breeds.Select(q => new BreedViewModel { Name = q.Name });
return PartialView("BreedSelectorPartial", names);
}
I’m not sure where I’m going wrong on this. I’ve spent hours trying to figure this out. Any solutions?
The first issue is that you are using a wrong overload of the
Ajax.BeginFormhelper (but that’s not causing the undesired behavior, it’s just a sidenote):whereas you need:
Now the real issue is that you need to put the
#resultsdiv outside the partial and inside your main view:Now of course if you only need to update the
WebGridportion then it would make more sense to leave only the grid inside the partial and move the form outside in the main view.So your main view now becomes:
and the only portion that needs to be updated is the
BreedSelectorPartial.cshtmlpartial which contains the grid: