返回错误并在ajax中查看

Hello everyone I want to know something is there possible. Using ajax send error message if occurred else send view. you can see my code. please help me what should I do. need some suggestions

here is my example

public ActionResult Test()
{
  string ErrorText = string.Empty;
  if(true)
    return View("PagedList");
  }
  else
  {
    return Json(new { errorMessage = ErrorText});
  }
}

Ajax:-

function Load(page) {

            $.ajax({
                type: "POST",
                url: page,
                xhrFields: {
                    withCredentials: true
                },
                success: function (data) {

                        $('#ReportLoad').empty();
                        $('#ReportLoad').append($.parseHTML(data));

                },
                 error: function (xhr, textStatus, exceptionThrown) {
                    $('#ReportErrors').empty();
                    $('#ReportErrors').html(JSON.parse(xhr.responseText));
                    $('#ReportErrors').show();
                },
                complete: function () {
                }
            });
        }

I don't know I am doing right way or not Can anyone suggest whats the best way to achieve this.

Return the error code as a HttpStatusCodeResult, if you just send a reply there is no way of Ajax knowing it is an error unless you check for specific text in the response.

 public ActionResult Test()
 {
      string ErrorText = string.Empty;
      if(true)
        return View("PagedList");
      }
      else
      {
        return new HttpStatusCodeResult(500, ErrorText);
      }
 }

And then detect this with Ajax:

function Load(page) {
            $.ajax({
                type: "POST",
                url: page,
                xhrFields: {
                    withCredentials: true
                },
                success: function (data) {

                        $('#ReportLoad').empty();
                        $('#ReportLoad').append($.parseHTML(data));

                },
                 error: function (xhr, textStatus, exceptionThrown) {
                    $('#ReportErrors').empty();
                    $('#ReportErrors').html(JSON.parse(xhr.responseText));
                    $('#ReportErrors').show();
                },
                complete: function () {
                },
                   statusCode: {
                   500: function(response)
                   {
                       alert("internal error");
                   }
                 }
            });
        }