You'll need to return multiple pieces of information for your response. Luckily, you can do that easily using JSON, and jQuery will automatically handle it for you if you tell it the response type is json. The object that you get into your ajax callback function will contain all the pieces of data you need as different properties.
I would suggest getting into the habit of returning a status code of "success" or "failure" with each ajax call, and a collection of errors with it. See this awesome blog post for more information on what I mean.
The reason for this is that an ajax call will always fundamentally "succeed", unless the server actually couldn't process the request and returned a failure http status code. If the result of the request is something like a validation error, but the server still returns some kind of text response, then the ajax call is still considered to have succeeded, even though the application operation failed.
So if, in your action method, instead of returning your html data in an action result, if you returned an object like this:
public class AjaxResponse
{
/// <summary>
/// Initializes a new instance of the <see cref="AjaxResponse"/> class.
/// This creates an AjaxResponse that by default indicates SUCCESS.
/// </summary>
public AjaxResponse()
{
Success = true;
Data = new List<object>();
}
/// <summary>
/// Initializes a new instance of the <see cref="AjaxResponse"/> class.
/// This creates an AjaxResponse that indicates FAILURE.
/// </summary>
/// <param name="exception">The exception.</param>
public AjaxResponse(Exception exception)
: this()
{
Success = false;
Errors = new [] { exception.Message };
}
/// <summary>
/// Initializes a new instance of the <see cref="AjaxResponse"/> class.
/// This creates an AjaxResponse that indicates SUCCESS.
/// </summary>
/// <param name="data">The data.</param>
public AjaxResponse(object data)
: this()
{
Data = data;
}
/// <summary>
/// Gets or sets a value indicating whether this <see cref="AjaxResponse"/> is success.
/// </summary>
/// <value><c>true</c> if success; otherwise, <c>false</c>.</value>
public bool Success
{
get; set;
}
/// <summary>
/// Gets or sets the data.
/// </summary>
/// <value>The data.</value>
public object Data
{
get; set;
}
/// <summary>
/// Gets or sets the errors.
/// </summary>
/// <value>The errors.</value>
public string[] Errors
{
get; set;
}
}
This will translate into a javascript object that has properties ".Success", ".Data" and ".Errors".
So if your validation code had populated the Errors array with all the validation errors, it would be easy for your ajax callback function to
determine that the intended purpose of the call had failed, because the SUCCESS property was set to "failure".
Get all the relevant error strings.
You could do this easily with this kind of pattern in your action methods:
try
{
instance.Validate();
return Json(new AjaxResponse(myHtmlData));
}
catch(Exception ex)
{
var response = new AjaxResponse(ex);
// Get your validation errors here, put them into
// your ajax response's Errors property.
return Json(response);
}