javascript - Redirect to View from AJAX call on error - Stack Overflow

Problem:I would like to load a partialview into a DIV when success, but in case an error happened I ne

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 the error: 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, use location.href='yourUrl'; – user3559349 Commented Jun 5, 2015 at 10:11
Add a ment  | 

1 Answer 1

Reset to default 6

Well 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);
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744899311a4599882.html

相关推荐

  • javascript - Redirect to View from AJAX call on error - Stack Overflow

    Problem:I would like to load a partialview into a DIV when success, but in case an error happened I ne

    1天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信