I am using MVC4. Unobtrusive JavaScript is included and mentioned in my project. Ajax scripts are uploaded on my shared homepage. FireBug does not report errors or 404.
I added to my page Text, which updates the contents of the field Hiddeninside my form Ajax. When the user presses a key and the event KeyUpfires, I forcibly submit the form Ajaxby calling:
$("form#ajaxForm").trigger("submit");
Where ajaxForm is the name of the Ajax form. It works great. The form is presented backstage, and my controller event fires. The model is updated with new data based on user input.
However, when it becomes strange, it is when the result returns to my form. All the contents of the tag of divmy form are replaced with the result - this is a full page.
<div id="basic-modal-content"">
<input id="breed" type="text"/>
<div>
<form id="ajaxForm" method="post" data-ajax-update="
<div id="results">
->THIS AREA IS GETTING REPLACED WITH THE ENTIRE FORM <-
<div id="basic-modal-content">
</div>
</div>
</form>
- Note that basic-modal content now contains itself, another basic-modal content. The result of the div should contain only the results of updates, and not the whole 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 am not sure where I am going wrong. I spent hours trying to figure it out. Any solutions?