Created
May 30, 2012 11:02
-
-
Save timiles/2835527 to your computer and use it in GitHub Desktop.
eg ModelState.Try("model.Email", () => user.ChangeEmail(newEmail));
This file contains 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
// credit: @mcintyre321 | |
public static class ModelStateDictionaryExtensions | |
{ | |
public static bool Try(this ModelStateDictionary ms, string key, Action action) | |
{ | |
try | |
{ | |
action(); | |
return true; | |
} | |
catch (Exception ex) | |
{ | |
ms.AddModelError(key, ex); | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment