Skip to content

Instantly share code, notes, and snippets.

@kouphax
Created October 18, 2010 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kouphax/632863 to your computer and use it in GitHub Desktop.
Save kouphax/632863 to your computer and use it in GitHub Desktop.
/// <summary>
/// Adds a model error using strongly typed lambda expressions
/// </summary>
public static void AddModelError<TModel>(
this ModelStateDictionary modelState,
Expression<Func<TModel, object>> method,
string message)
{
if (method == null)
{
throw new ArgumentNullException("method");
}
MemberExpression mce = method.Body as MemberExpression;
string property = mce.Member.Name;
modelState.AddModelError(property, message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment