Last active
October 13, 2015 23:47
-
-
Save warrendodsworth/d7407514715cf5142b35 to your computer and use it in GitHub Desktop.
Validation Summary from Web Api 2 BadRequest response- Format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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