So the page needs to refresh after an ajax call…..
In the controller action build the redirect url and include any route parameters that are needed. The Url is returned in the Json object to the calling javascript along with any other values e.g. the result of a database update.
[HttpPost]
public ActionResult Cancel( //parameters here )
{
var redirectUrl = new UrlHelper(Request.RequestContext).Action("Index", "user", new { } );
try
{
// perform some action
}
catch (Exception)
{
return Json(new { Url = redirectUrl, status = "Error" });
}
return Json(new { Url = redirectUrl, status ="OK" });
}
In the Ajax call check the returned data from the action method. If the action was successful redirect, if not display a message or something else if you need.
var UserModel = {
// initialise model fields
};
$.ajax({
url: "@Url.Content("~/user/cancel")",
data: JSON.stringify(UserModel),
type: 'POST',
contentType: 'application/json; charset=utf-8',
success: function (data) {
if (data.status === 'OK') {
window.location.href = data.Url
}
else {
alert("The status cannot be updated at this time");
}
}
});
Thanks for reading.

Reblogged this on Dinesh Ram Kali..