I have the following jquery code in my view
<script type="text/javascript">
$(document).ready(function () {
$("#dialog-confirm").dialog({
autoOpen: false,
modal: true,
resizable: false,
width: '500px'
});
$(".deleteLink").click(function (e) {
e.preventDefault();
var targetUrl = $(this).attr("href");
var dID = $(this).attr("id");
$("#dialog-confirm").dialog({
buttons: {
"Confirm": function () {
$.ajax({
url: '@Url.Action("DeleteSession")',
type: 'POST',
data: { id: dID },
success: function (data) {
window.location.herf = data.redirectToUrl;
}
});
},
"Cancel": function () {
$(this).dialog("close");
}
}
});
$("#dialog-confirm").dialog("open");
});
});
</script>
This link that triggers the dialog box is;
@Html.ActionLink("Delete", "", new { id = s.ID },new { @class = "deleteLink", id = s.ID})
The controller method DeleteSession returns a Json result.
Controller:
[HttpPost]
public JsonResult DeleteSession(int id)
{
try
{
sRep.DeleteSession(id);
return Json(new {success = true, redirectToUrl = Url.Action("Index")});
}
catch (Exception e)
{
return Json(new {success = false, redirectToUrl = Url.Action("DisplayError", new { eerror =
"Unable to delete the course. " + "Internal error: " + e.Message})});
}
}
I have inspected the Json result and it seems fine. The only problem is window.location.herf = data.redirectToUrl; its not working. The page is not redirected and the dialog box is still on the screen.
any idea what i am doing wrong?
I think you meant to do
window.location.hrefYour original code:
Should be changed to: