Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6852195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:20:31+00:00 2026-05-27T01:20:31+00:00

If a normal page load errors I can report the exception details to the

  • 0

If a normal page load errors I can report the exception details to the user via the Error view and HandleErrorInfo model.

If an ajax call expecting a Json result errors, I can explicitly handle the error and pass details to the client:

public JsonResult Whatever()
{
    try
    {
        DoSomething();
        return Json(new { status = "OK" });
    }
    catch (Exception e)
    {
        return Json(new { status = "Error", message = e.Message });
    }
}

So, my problem, I can’t see any way to report error details from an Ajax call to an action returning a partial view.

$.ajax({
    url: 'whatever/trevor',
    error: function (jqXHR, status, error) {
        alert('An error occured: ' + error);
    },
    success: function (html) {
        $container.html(html);
    }
});

This will only report an Http error code (e.g. Internal Server Error) which is not helpful to the client. Is there some clever trick to pass either a successful PartialView (html) result or an error message?

Explicitly evaluating the html from the ViewResult and returning it as part of a Json object along with a status seems too smelly. Is there an established pattern for handling this scenario?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:20:31+00:00Added an answer on May 27, 2026 at 1:20 am

    Controller action:

    public ActionResult Foo()
    {
        // Obviously DoSomething could throw but if we start 
        // trying and catching on every single thing that could throw
        // our controller actions will resemble some horrible plumbing code more
        // than what they normally should resemble: a.k.a being slim and focus on
        // what really matters which is fetch a model and pass to the view
    
        // Here you could return any type of view you like: JSON, HTML, XML, CSV, PDF, ...
    
        var model = DoSomething();
        return PartialView(model);
    }
    

    Then we define a Global error handler for our application:

    protected void Application_Error(object sender, EventArgs e)
    {
        var exception = Server.GetLastError();
        var httpException = exception as HttpException;
        Response.Clear();
        Server.ClearError();
    
        if (new HttpRequestWrapper(Request).IsAjaxRequest())
        {
            // Some error occurred during the execution of the request and 
            // the client made an AJAX request so let's return the error
            // message as a JSON object but we could really return any JSON structure
            // we would like here
    
            Response.StatusCode = 500;
            Response.ContentType = "application/json";
            Response.Write(new JavaScriptSerializer().Serialize(new 
            { 
                errorMessage = exception.Message 
            }));
            return;
        }
    
        // Here we do standard error handling as shown in this answer:
        // http://stackoverflow.com/q/5229581/29407
    
        var routeData = new RouteData();
        routeData.Values["controller"] = "Errors";
        routeData.Values["action"] = "General";
        routeData.Values["exception"] = exception;
        Response.StatusCode = 500;
        if (httpException != null)
        {
            Response.StatusCode = httpException.GetHttpCode();
            switch (Response.StatusCode)
            {
                case 404:
                    routeData.Values["action"] = "Http404";
                    break;
                case 500:
                    routeData.Values["action"] = "Http500";
                    break;
            }
        }
    
        IController errorsController = new ErrorsController();
        var rc = new RequestContext(new HttpContextWrapper(Context), routeData);
        errorsController.Execute(rc);
    }
    

    Here’s how the ErrorsController used in the global error handler could look like. Probably we could define some custom views for the 404 and 500 actions:

    public class ErrorsController : Controller
    {
        public ActionResult Http404()
        {
            return Content("Oops 404");
        }
    
        public ActionResult Http500()
        {
            return Content("500, something very bad happened");
        }
    }
    

    Then we could subscribe for a global error handler for all AJAX errors so that we don’t have to repeat this error handling code for all AJAX requests but if we wanted we could repeat it:

    $('body').ajaxError(function (evt, jqXHR) {
        var error = $.parseJSON(jqXHR.responseText);
        alert('An error occured: ' + error.errorMessage);
    });
    

    And finally we fire an AJAX request to the controller action that we hope will return an HTML partial in this case:

    $.ajax({
        url: 'whatever/trevor',
        success: function (html) {
            $container.html(html);
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Are ajax requests more resource-intensive than a normal page load? For example, I have
Can anyone help in this php page navigation script switch on counting normal serial
This page I use crystal report to generate a report, but if 10 user
I am struggling with a problem about automatic AJAX load of page. I have
I have a normal asp.net page containing some code that I want to measure
I've got a page with a normal form with a submit button and some
I have a web page that doesnt fit screen in normal resolution, so I
My app has normal users: those which come through a typical signup page facebook(FB)
I have acustom sharepoint page. on my machine I can debug the code normaly
Under normal circumstances, a VB.NET application of mine can check the ClientName environmental variable

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.