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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:05:31+00:00 2026-05-17T02:05:31+00:00

Suppose you have the following controller action [HttpPost] public ActionResult Save( CustomerModel model )

  • 0

Suppose you have the following controller action

[HttpPost]
public ActionResult Save( CustomerModel model )
{
    if (!ModelState.IsValid) {
        //Invalid - redisplay form with errors
        return PartialView("Customer", model);
    }
    try {
        //
        // ...code to save the customer here...
        //
        return PartialView( "ActionCompleted" );
    }
    catch ( Exception ex ) {
        ActionErrorModel aem = new ActionErrorModel() { 
            Message = ex.Message 
        };
        return PartialView( "ActionError", aem );
    }
}

And suppose you call this action using jQuery:

$.ajax({
    type: "post",
    dataType: "html",
    url: "/Customer/Save",
    sync: true,
    data: $("#customerForm").serialize(),
    success: function(response) {
        /*
             ??????
        */
    },
    error: function(response) {
    }
});

I would like to be able to distinguish between the results I am getting to handle them in different ways on the client. In other words how can I understand that the action

  • returned the same model because has not passed validation
  • returned one of the views that represents error info/messages

Any suggestion?

  • 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-17T02:05:32+00:00Added an answer on May 17, 2026 at 2:05 am

    One way to handle this is to append a custom HTTP header to indicate in which case we are falling:

    [HttpPost]
    public ActionResult Save( CustomerModel model )
    {
        if (!ModelState.IsValid) {
            //Invalid - redisplay form with errors
            Response.AppendHeader("MyStatus", "case 1");
            return PartialView("Customer", model);
        }
        try {
            //
            // ...code to save the customer here...
            //
            Response.AppendHeader("MyStatus", "case 2");
            return PartialView( "ActionCompleted" );
        }
        catch ( Exception ex ) {
            ActionErrorModel aem = new ActionErrorModel() { 
                Message = ex.Message 
            };
            Response.AppendHeader("MyStatus", "case 3");
            return PartialView( "ActionError", aem );
        }
    }
    

    And on the client side test this header:

    success: function (response, status, xml) {
        var myStatus = xml.getResponseHeader('MyStatus');
        // Now test the value of MyStatus to determine in which case we are
    }
    

    The benefit of this is that the custom HTTP header will always be set in the response no matter what content type you’ve returned. It will also work with JSON, XML, …

    Remark 1: To avoid cluttering you controller action with all those Response.AppendHeader instructions you could write a custom ActionResult allowing you to directly specify the value of this header so that you simply return this.MyPartialView("Customer", model, "case 1")

    Remark 2: Remove this sync: true attribute from the request because it makes my eyes hurt (in fact I think you meant async: 'false').

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.