I\'m trying to load a view after an ajax call. After the ajax call my action method will return a view
which is going to be loaded after the call is success.
AJAX calls stay on the same page so RedirectToAction does not work. You need to modify your controller to return JSON, for example
[HttpPost]
public JsonResult RegisterAndLogin(UserRegisterViewModel model)
{
bool successToStoreData = SomeMethod(model);
if (successToStoreData)
{
return null; // indicates success
}
else
{
return Json("Your error message");
}
}
and modify the AJAX function
$.ajax({
type: 'Post',
dataType: "json",
url: url,
contentType: 'application/json',
data: JSON.stringify(userRegisterViewModel),
success: function(message) {
if (message) {
$('yourSpanSelector').text(message); // display the error message in the span tag
} else {
window.location.href='/YourController/YourAction' // redirect to another page
}
}
})