Problem:
I would like to load a partialview into a DIV when success, but in case an error happened I need to redirect to the view home page. The issue is that the Redirect to Action is also loaded into the div resultdisplay
.
How should I manage the errorhandling to Redirect to fullviews in case of error?
Code:
Ajax call:
function GetFilteredValuesCallback(values) {
var data = JSON.stringify(values);
var url = '/Controller/Action';
$.ajax({
type: 'GET',
url: url,
data: { filter: data },
success: function (result) {
$('#resultDisplay').html(result);
}
});
}
Action:
public ActionResult Action(string filter)
{
MyModel model = null;
try
{
// Do stuff with my model ...
throw new Exception("ERROR!");
}
catch (Exception ex)
{
// In case of error redirect to home page
return RedirectToAction("Index");
}
return PartialView("_PartialView", model);
}
Problem:
I would like to load a partialview into a DIV when success, but in case an error happened I need to redirect to the view home page. The issue is that the Redirect to Action is also loaded into the div resultdisplay
.
How should I manage the errorhandling to Redirect to fullviews in case of error?
Code:
Ajax call:
function GetFilteredValuesCallback(values) {
var data = JSON.stringify(values);
var url = '/Controller/Action';
$.ajax({
type: 'GET',
url: url,
data: { filter: data },
success: function (result) {
$('#resultDisplay').html(result);
}
});
}
Action:
public ActionResult Action(string filter)
{
MyModel model = null;
try
{
// Do stuff with my model ...
throw new Exception("ERROR!");
}
catch (Exception ex)
{
// In case of error redirect to home page
return RedirectToAction("Index");
}
return PartialView("_PartialView", model);
}
Share
Improve this question
edited Jun 10, 2015 at 5:58
royhowie
11.2k14 gold badges53 silver badges67 bronze badges
asked Jun 5, 2015 at 10:07
blfuentesblfuentes
2,8275 gold badges46 silver badges84 bronze badges
2
-
Server-side
RedirectToAction
returns a redirect response to the browser, which Ajax then promptly ignores. You need to return metadata to your Ajax call, so that it then knows to change the page to a new URL (or use an error instead and redirect in theerror:
callback). – iCollect.it Ltd Commented Jun 5, 2015 at 10:11 -
Ajax calls stay on the same page. Instead of catching the exception, let it happen and then in the ajax
.fail()
callback, uselocation.href='yourUrl';
– user3559349 Commented Jun 5, 2015 at 10:11
1 Answer
Reset to default 6Well instead of redirecting in controller you can just send information back to client ajax request and handle redirection here as below:
function GetFilteredValuesCallback(values) {
var data = JSON.stringify(values);
var url = '/Controller/Action';
$.ajax({
type: 'GET',
url: url,
data: { filter: data },
success: function (result) {
if(result.message==="Failed")
location.href = "yoururl"
else
$('#resultDisplay').html(result);
},
error:function(result)
{
//handle any errors
}
});
}
Controller
public ActionResult Action(string filter)
{
MyModel model = null;
try
{
// Do stuff with my model ...
throw new Exception("ERROR!");
}
catch (Exception ex)
{
// In case of error
return Json(new{message="Failed"}, JsonRequestBehavior.AllowGet); //Send a message back
}
return PartialView("_PartialView", model);
}
UPDATE
Thanks @StephenMuecke for the suggestion to handle this in more elegant way which can be done as below:
JS
function GetFilteredValuesCallback(values) {
var data = JSON.stringify(values);
var url = '/Controller/Action';
$.ajax({
type: 'GET',
url: url,
data: { filter: data },
success: function (result) {
$('#resultDisplay').html(result);
},
error:function(result)
{
location.href("yoururl");
}
});
}
Controller
public ActionResult Action(string filter)
{
MyModel model = null;
try
{
// Do stuff with my model ...
throw new Exception("ERROR!");
}
catch (Exception ex)
{
// In case of error
return new HttpStatusCodeResult(500);
}
return PartialView("_PartialView", model);
}