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

  • Home
  • SEARCH
  • 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 3307812
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:26:58+00:00 2026-05-17T21:26:58+00:00

if i call a ajax request using jquery to an asp.net mvc controller action

  • 0

if i call a ajax request using jquery to an asp.net mvc controller action and inside that action an exception gets thrown, what is the best way to capture that and show something to the user.

right now my loading image just goes on forever . . . .

here is an example of my controller code:

  public ActionResult BatchAdd(int[] sharepointKeys)
  {
           MyViewModel vm = GetViewModel();
            return PartialView("MyPartialView", vm);
  }
  • 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-17T21:26:58+00:00Added an answer on May 17, 2026 at 9:26 pm

    c#

    public JsonResult Action(...)
    {
        bool error = false;
        string msg = "";
        var data = "your data";
    
        try
        {
           //you code
        }
        catch (Exception ex)
        {
            error = true;
            msg = "Error ...";
        }
    
        return Json(new { error = error, msg = ms, data = data });
    }
    

    js:

    $.ajax({
       type: "POST",
       url: "Controller/Action",
       data: "...",
       beforeSend: function(xhr){
            $("#showError").empty();
       },
       error: function(xhr, ts, et){
            $("#showError").append("<div>Error</div>");
       },
       success: function(response){
    
         if (response.error) {
            $("#showError").append("<div>"+response.msg+"</div>");
         } 
         else {
            // code ok response
         }
    
    
       }
     });
    

    With return ActionResult:

    Index.asp:

    <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
        Home Page
    </asp:Content>
    
    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    
        <div id="showError"></div>
        <div id="dataHtml"></div>
    
        <button id="example">Example</button>
        <script type="text/javascript">
    
            $(function () {
    
                $("#example").bind('click', function (ex) {
    
                    $.ajax({
                        type: "POST",
                        url: "Home/BatchAdd",
                        data: { sharepointKeys: [1, 2, 3] },
                        beforeSend: function (xhr) {
                            $("#showError").empty();
                        },
                        error: function (xhr, ts, et) {
                            $("#showError").append("<div>Error</div>");
                        },
                        success: function (response) {
    
                            if ($(response).find("#error-message").length > 0) {
                                $("#showError").append("<div>" + $(response).find("#error-message").html() + "</div>");
                            }
                            else {
                                $("#dataHtml").html("<div>" + $(response).find("#container-response-ok").html() + "</div>");
                            }
    
    
                        }
                    });
                });
    
            });
    
        </script>
    
    </asp:Content>
    

    Site.master:

    <%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
        <link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
        <script src="../../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    </head>
    
    <body>
        <div class="page">
    
            <div id="header">
    
                <div id="menucontainer">
                    <ul id="menu">              
                        <li><%= Html.ActionLink("Home", "Index", "Home")%></li>
                        <li><%= Html.ActionLink("About", "About", "Home")%></li>
                    </ul>
                </div>
            </div>
    
            <div id="main">
                <asp:ContentPlaceHolder ID="MainContent" runat="server" />
                <div id="footer"></div>
            </div>
        </div>
    </body>
    </html>
    

    MyPartialView.ascx:

    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MvcApplication1.Controllers.MyViewModel>" %>
    <div>
        <% if (Model.Error)
        { %>
        <div id="error-message"><%= Model.Message %></div>
        <% } else { %>
        <div id="container-response-ok">
          <div> all ok </div>
        </div>
        <% } %>
    </div>
    

    HomeController.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    namespace MvcApplication1.Controllers
    {
        [HandleError]
        public class HomeController : Controller
        {
            public ActionResult Index()
            {
                ViewData["Message"] = "Welcome to ASP.NET MVC!";
    
                return View();
            }
    
            public ActionResult BatchAdd(int[] sharepointKeys)
            {
                MyViewModel vm = GetViewModel();
    
                try
                {
                    vm.Error = true;
                    vm.Message = "Error testing";
                }
                catch (Exception ex)
                {
                    vm.Error = true;
                    vm.Message = "Errro";
                }
    
                return PartialView("MyPartialView", vm);
            }
    
            public ActionResult About()
            {
                return View();
            }
    
            private MyViewModel GetViewModel()
            {
                return (new MyViewModel());
            }
        }
    
    
    
        public class MyViewModel
        {
            public bool Error { get; set; }
            public string Message { get; set; }
    
            public MyViewModel()
            {
                this.Error = false;
                this.Message = "";
            }
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to do an AJAX call (via JQuery) that will initiate a fairly
I have coded some JavaScript to perform an ajax call in an asp.net application.
I have this code that performs an ajax call and loads the results into
Let's say I have the following jQuery AJAX call: $.ajax({ type: POST, url: MyUrl,
Is it possible to use a jQuery ajax call to perform Forms Authentication with
I'm attempting to issue two concurrent AJAX requests. The first call (/ajax_test1.php) takes a
Below is my $.ajax call, how do I put a selects (multiple) selected values
I would like to make an ajax call to a different server (same domain
I'm trying to make an ajax call to grab session data to insert into
I am inserting the HTML response from an AJAX call into my page, but

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.