I have an edit partial view to load in a dialog, which works the first time it's called, the edit then saves the data, great.
However if you don't refresh the page, which I don't want to have to do, and click 'edit' again, you are redirected to the partial page I am trying to load.
It looks like the AJAX is being ignored and the Action method in the controller is being called instead.
I have unobtrusive added through the bundles in my layout page, which the main view uses, the partial doesn't, but I don't believe it needs to.
I have also tried using preventDefault
and different approached of return false;
with no success, like the below:
editBtn.click(function (e) {
editDialogPlaceholder.load(this.href, function () {
e.preventDefault(); //tried
$(this).dialog('open');
return false; //tried
})
return false;
});
How can I stop this?
AJAX/JS
$(function () {
var editBtn = $('.editLink');
var editDialogPlaceholder = $('#editDialogUI');
editBtn.button();
// build the dialog form
editDialogPlaceholder.dialog({
autoOpen: false,
modal: true,
resizable: false,
open: function (event, ui) {
$(this).load('@Url.Action("Edit","Announcements")');
},
buttons: {
"Save": function () {
saveAnnouncement();
fetchList();
$(this).dialog("close");
},
"Close": function () {
$(this).dialog("close");
}
}
});
// on click load the dialog form, return false to stop redirect
editBtn.click(function () {
editDialogPlaceholder.load(this.href, function () {
$(this).dialog('open');
})
return false;
});
});
//post data back to the controller to save to the db
function saveAnnouncement() {
$.ajax({
url: '@Url.Action("Edit","Announcements")',
type: 'POST',
data: $('form').serialize()
})
}
function fetchList() {
var aList = $('#announcementList');
aList.load('@Url.Action("Fetch")');
}
Edit action method
[HttpGet]
public ActionResult Edit(int id)
{
var announcementModel = (from a in db.DbAnnouncement
where a.Id == id
select a).SingleOrDefault();
return PartialView("Edit", announcementModel);
}
Html.ActionLinks being used to open the dialog - second time of clicking it redirects to the partial specified in the controller.
<div class="announcementTableCell">
@Html.ActionLink("Edit", "Edit", new { id = Model[i].Id }, new { @class = "editLink" })
@Html.ActionLink("Delete", "Delete", new { Model[i].Id }, new { @class = "dltLink" })
</div>