how to return an error in an Ajax scenario from ASP.NET MVC action

Posted by mohang on Stack Overflow See other posts from Stack Overflow or by mohang
Published on 2010-05-14T07:27:05Z Indexed on 2010/05/16 16:10 UTC
Read the original article Hit count: 235

Filed under:
|
|
|

I am using ASP.NET MVC with jquery. I have the following MVC Action that returns a partial page on Success. On Application Error, I am not sure what to send it for correctly handling it at the client side.

public ActionResult LoadFilterSet(int filterSetId)
        {
            try
            {
                BreadCrumbManager bcManager = this.ResetBreadCrumbManager(this.BreadCrumbManagerID);
                GeneralHelper.LoadBreadCrumbManager(bcManager, filterSetId);

                ViewData["BreadCrumbManager"] = bcManager;

                return View("LoadFilterSet");
            }
            catch (Exception ex)
            {
                return Content("");
            }
        }

Following is my jquery ajax call. Notice that I am checking for the data length to make sure there are no errors. Please suggest me a better way of doing this.

$.ajax({
            type: "GET",
            dataType: "html",
            async: true,
            data: ({ filterSetId: selectedId }),
            url: link,
            contentType: "text/html; charset=utf-8",

            success: function(data, textStatus) {
                if (data.length > 0) {

                    // Clear the local filters first.
                    clearLocalFilters();

                    $('td.selected-filters table.filters-display').append(data);
                }
            }

        });

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about mvc