0

I'm rendering a partial view inside of a view:

@{
    Html.RenderAction("PartialViewAction", "SomeController");
}  

Is there a way to redirect the user to an error page if the partial view action encounters an error?

EDIT: I'm looking for a server side kind of redirection. This partial view is not loaded with AJAX. It is rendered server side into a "big" view. The "big" view has no idea that the partial view errored out.

Dimskiy
  • 5,233
  • 13
  • 47
  • 66

1 Answers1

0

Depending on your logic, you might be able to control your application flow by using jQuery.ajax() to handle errors.

// this will render the GET request on page load
$(function() {
    $.ajax({
        url: '/Some/PartialViewAction',
        type: 'GET',
        dataType: 'json',                            /*edit*/
        success: function(xhr_data) {                /*edit*/
            // the following assumes you wrap 
            // your partial view in div id="myDiv"
            $('#myDiv').html(xhr_data.html);         /*edit*/
            $('#myErrorDiv').html(xhr_data.error);   /*edit*/
        },
        error: function() {
            window.location.href='/Some/Error';
            // or similar page redirecting technique
        }
    });
});

This will handle an error in the GET, but of course if you were to return some JSON or some other indicator in your action method, you could use the same window.location.href... in the success callback function.

Edit

Also see above edits to $.ajax

Your controller could do something like this:

public ActionResult PartialViewAction() {
    // handle error
    string message = "Evacuate, Immediately!";
    // not certain the html will render correctly,
    // but you could encode/parse/whatever easily enough
    return Json(new { html = "<div>some html</div>", error = message },
        JsonRequestBehavior.AllowGet);
}
David Fox
  • 10,603
  • 9
  • 50
  • 80
  • Thanks! I got AJAX covered. I edited my question a little to be more specific. – Dimskiy Jun 01 '11 at 20:35
  • @Dimskiy I don't think you'll be able to handle redirecting without some sort of client side interaction if the error exists inside an Ajax request. – David Fox Jun 01 '11 at 20:39
  • I think there has to be some kind of way. But ok, what if the partial view encountered an error and it got logged and handled on the server. Now, the last thing to do is to display some kind of user friendly message in the UI. How would you go about this? – Dimskiy Jun 01 '11 at 20:51
  • @Dimskiy You would still need that data wrapped and returned in your partial postback. You could use JSON and perhaps wrap the HTML of the partial view in one variable and some message in another. I'll edit to demonstrate. – David Fox Jun 01 '11 at 20:54
  • In this case, instead of "hardcoding" html to the JSON object, why not just create a special "error partial view" and in case of an error just render that? I think that would be better then building a JSON and everything. – Dimskiy Jun 01 '11 at 21:05