title: Set ajax / JSon error details in a controller
body: <p>If I have the following in my view:</p>
  
  <pre><code>$.ajax({
      url: '@PathHelper.FullyQualifiedApplicationPath(Request)' + 'Home/asdasd',
      type: 'POST', dataType: 'json',
      contentType: "application/x-www-form-urlencoded;charset=UTF-8",
      data: {
              searchForIt: document.getElementById('searchValue').value
          },
      error: OnError,
      success: function (data) {
          // Do Stuff
      }
  });
  </code></pre>
  
  <p>and the error function OnError is:</p>
  
  <pre><code>function OnError(xhr, errorType, exception) {
      var responseText;
      $("#dialog").html("");
  
      try {
          responseText = jQuery.parseJSON(xhr.responseText);
          $("#dialog").append("&lt;div&gt;&lt;b&gt;An error has occurred, please inform Paul Zahra quoting the following details:&lt;/b&gt;&lt;/div&gt;");
          $("#dialog").append("&lt;div&gt;&lt;b&gt;" + errorType + " " + exception + "&lt;/b&gt;&lt;/div&gt;");
          $("#dialog").append("&lt;div&gt;&lt;u&gt;Exception&lt;/u&gt;:&lt;br /&gt;&lt;br /&gt;" + responseText.ExceptionType + "&lt;/div&gt;");
          $("#dialog").append("&lt;div&gt;&lt;u&gt;StackTrace&lt;/u&gt;:&lt;br /&gt;&lt;br /&gt;" + responseText.StackTrace + "&lt;/div&gt;");
          $("#dialog").append("&lt;div&gt;&lt;u&gt;Message&lt;/u&gt;:&lt;br /&gt;&lt;br /&gt;" + responseText.Message + "&lt;/div&gt;");
      } catch (e) {
          responseText = xhr.responseText;
          $("#dialog").html(responseText);
      }
  }
  </code></pre>
  
  <p>In the controller how do I set the parameters of the JSon error so that I can give custom details such as "You entered invalid data." or "The database has a fit, please try again."?;</p>
  
  <pre><code>OnError(xhr, errorType, exception)
  </code></pre>
  
  <p>P.S. In the controller in order to get ajax in the view to think an error has occured I merely use:</p>
  
  <pre><code>Response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest;
  </code></pre>
  
  <p>P.P.S. I know the following but it doesn't explain how I set it from an mvc controller.</p>
  
  <p>"<strong>error</strong>
  Type: Function( jqXHR jqXHR, String textStatus, String errorThrown )
  A function to be called if the request fails. The function receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred. Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror". When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error."</p>
  
  <p>"For backward compatibility with XMLHttpRequest, a jqXHR object will expose the following properties and methods:</p>
  
  <pre><code>readyState
  status
  statusText
  responseXML and/or responseText when the underlying request responded with xml and/or text, respectively
  setRequestHeader(name, value) which departs from the standard by replacing the old value with the new one rather than concatenating the new value to the old one
  getAllResponseHeaders()
  getResponseHeader()
  statusCode()
  abort()"
  </code></pre>