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 8706641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:39:48+00:00 2026-06-13T03:39:48+00:00

I have a method in a controller that looks like this: [HttpPost] public void

  • 0

I have a method in a controller that looks like this:

[HttpPost]
public void UnfavoriteEvent(int id)
{
    try
    {
        var rows = _connection.Execute("DELETE UserEvent WHERE UserID = (SELECT up.UserID FROM UserProfile up WHERE up.UserName = @UserName) AND EventID = @EventID",
            new { EventID = id, UserName = User.Identity.Name });
        if (rows != 1)
        {
            Response.StatusCode = 500;
            Response.Status = "There was an unknown error updating the database.";
            //throw new HttpException(500, "There was an unknown error updating the database.");
        }
    }
    catch (Exception ex)
    {
        Response.StatusCode = 500;
        Response.Status = ex.Message;
        //throw new HttpException(500, ex.Message);
    }
}

And as you can see I’ve tried a couple of different ways to throw this error back. In the JavaScript I have the following block to call this method:

var jqXHR;
if (isFavorite) {
    jqXHR = $.ajax({
        type: 'POST',
        url: '/Account/UnfavoriteEvent',
        data: { id: $("#EventID").val() }
    });
}
else {
    jqXHR = $.ajax({
        type: 'POST',
        url: '/Account/FavoriteEvent',
        data: { id: $("#EventID").val() }
    });
}

jqXHR.error = function (data) {
    $("#ajaxErrorMessage").val(data);
    $("#ajaxError").toggle(2000);
};

Now, what I want to do is get the error that occurs thrown back to the jqXHR.error function so that I can handle it properly.

Currently the code that is uncommented throws an exception saying that the text I’m placing in Status is not allowed, and the commented code actually returns the standard error page as the response (not surprising really).

So, I have a couple of questions:

  1. How do I throw the error properly?
  2. What does the Response.Status property do?

Thanks all!

  • 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-06-13T03:39:49+00:00Added an answer on June 13, 2026 at 3:39 am

    You will be abled to get the response status from the javascript side, doing the following:

    $.ajax({
        type: 'POST',
        url: '/Account/UnfavoriteEvent',
        data: { id: $("#EventID").val() },
        success: function(data, textStatus, jqXHR) {
            // jqXHR.status contains the Response.Status set on the server
        },
        error: function(jqXHR, textStatus, errorThrown) {
            // jqXHR.status contains the Response.Status set on the server
        }});
    

    As you can see, you must pass the function for error to the ajax function… in you example, you are setting the function to the error property of jqXHR with no effect at all.

    Documentation on ajax events

    • jQuery – Ajax Events

    jQuery docs say that the error string will come in the errorThrown parameter.

    Do not use Response

    Instead, you should return HttpStatusCodeResult:

    [HttpPost]
    public void UnfavoriteEvent(int id)
    {
        try
        {
            var rows = _connection.Execute("DELETE UserEvent WHERE UserID = (SELECT up.UserID FROM UserProfile up WHERE up.UserName = @UserName) AND EventID = @EventID",
                new { EventID = id, UserName = User.Identity.Name });
            if (rows != 1)
            {
                return new HttpStatusCodeResult(500, "There was an unknown error updating the database.");
            }
        }
        catch (Exception ex)
        {
            return new HttpStatusCodeResult(500, ex.Message);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a controller method that looks like this: [AcceptGet] public ActionResult Index(SecurityMatrixIndexViewModel model)
I have a basic Spring MVC controller that looks like this: @Controller public void
I have a controller method in ASP.NET MVC that looks like this: public ActionResult
I have an action method that looks like this: public ActionResult Index(string message) {
In ASP.NET MVC I have a controller that looks somehow like this: public class
I have a controller method that looks like public ViewResult Index(string id, string participant=,
I have a controller method that is a little something like this: def suggestions
I have a controller that looks like this for importing xml to my site:
I have a route in my code that looks like this: map.app 'applications/:title_header', :controller
I have a Security class that looks like this: package controllers; import play.Logger; public

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.