Skip to content

Instantly share code, notes, and snippets.

@warrendodsworth
Last active October 13, 2015 23:47
Show Gist options
  • Select an option

  • Save warrendodsworth/d7407514715cf5142b35 to your computer and use it in GitHub Desktop.

Select an option

Save warrendodsworth/d7407514715cf5142b35 to your computer and use it in GitHub Desktop.
Validation Summary from Web Api 2 BadRequest response- Format
// Read and Format Error messages
// Using custom ajax request
// You may need to get to modelState differently depending on how you make your ajax call
// Attached to the jQuery namespace using $.summary = function
$.summary = function ( res ) {
var errors = [], validationSummary;
// eg - res.data.modelState
if ( res.responseJSON.modelState ) {
//If BadRequest(ModelState)
$.each( res.responseJSON.modelState, function ( i, propertyErrors ) {
errors.push.apply( errors, propertyErrors );
} );
validationSummary = errors.join( '</br>' );
} else {
//else BadRequest("Message")
errors.push( res.responseJSON.message );
validationSummary = errors.join( '' );
}
if ( validationSummary )
validationSummary = validationSummary.trim();
return validationSummary;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment