• Code
  • Tags
  • Users
  • Titles
  • Log in
  • Feedback
  • FAQ
Share Code
Welcome to ForkCan.com

ForkCan is all about sharing code in a social way.

Discuss, debate or argue with other devs about their or your own code.

Give other devs feedback or make a Fork (Make a better version of a shared code).

Rate the code, if you use the code mark it as used so others can see if the shared code is used by someone.

Help each other to be better devs and to be more productive.


Features not working yet:

Flag a post


QR Code

Tiny Url

http://4kcan.com/s/MjM5

Related Code
Fork of GZIP your Actions in MVC if not used as standard in IIS7 or only use IIS6
ActionFilter to Handle and Log Error for each Controller, ASP.NET MVC
GZIP your Actions in MVC if not used as standard in IIS7 or only use IIS6

Removing properties from the model state the typed way in ASP.NET MVC

An extension method to remove properties from the model state the typed way in ASP.NET MVC

1
848 0 0 0 0 2

/// <summary>
/// Extensions for the <see cref="ModelStateDictionary"/> class.
/// </summary>
public static class ModelStateDictionaryExtensions
{
    /// <summary>
    /// Removes the specified member from the  <see cref="ModelStateDictionary"/>.
    /// </summary>
    /// <param name="me">Me.</param>
    /// <param name="lambdaExpression">The lambda expression.</param>
    public static void Remove<TViewModel>(
        this ModelStateDictionary me,
        Expression<Func<TViewModel, object>> lambdaExpression)
    {
        me.Remove(GetPropertyName(lambdaExpression));
    }

    /// <summary>
    /// Gets the name of the property.
    /// </summary>
    /// <param name="lambdaExpression">The lambda expression.</param>
    /// <returns></returns>
    private static string GetPropertyName(this Expression lambdaExpression)
    {
        var e = lambdaExpression;

        while (true)
        {
            switch (e.NodeType)
            {
                case ExpressionType.Lambda:
                    e = ((LambdaExpression)e).Body;
                    break;

                case ExpressionType.MemberAccess:
                    var propertyInfo =
                        ((MemberExpression)e).Member
                        as PropertyInfo;

                    return propertyInfo != null
                               ? propertyInfo.Name
                               : null;

                case ExpressionType.Convert:
                    e = ((UnaryExpression)e).Operand;
                    break;

                default:
                    return null;
            }
        }
    }
}

Share: twitter | facebook   Action: used | fork | flag

asp.net

Mark 'asp.net' tag as 'like'

Mark 'asp.net' tag as 'ignore'

mvc

Mark 'mvc' tag as 'like'

Mark 'mvc' tag as 'ignore'


 @lennybacon
222
September 08, 2010 12:27 AM
edited September 08, 2010 12:54 AM

Fork

 Removing properties from the model state the typed way in ASP.NET MVC -  @lennybacon Wednesday 08, 2010 12:27 AM


0 Feedback


You must log in before you can give any feedback


2 Discussion(s)

Newest Oldest
1

Added the AddError stuff...

    /// <summary>
    /// Adds the model error.
    /// </summary>
    /// <typeparam name="TViewModel">The type of the view model.</typeparam>
    /// <param name="me">Me.</param>
    /// <param name="lambdaExpression">The lambda expression.</param>
    /// <param name="errorMessage">The error message.</param>
    public static void AddModelError<TViewModel>(
        this ModelStateDictionary me,
        Expression<Func<TViewModel, object>> lambdaExpression,
        string errorMessage)
    {
        me.AddModelError(GetPropertyName(lambdaExpression), errorMessage);
    }

    /// <summary>
    /// Adds the model error.
    /// </summary>
    /// <typeparam name="TViewModel">The type of the view model.</typeparam>
    /// <param name="me">Me.</param>
    /// <param name="lambdaExpression">The lambda expression.</param>
    /// <param name="exception">The exception.</param>
    public static void AddModelError<TViewModel>(
        this ModelStateDictionary me,
        Expression<Func<TViewModel, object>> lambdaExpression,
        Exception exception)
    {
        me.AddModelError(GetPropertyName(lambdaExpression), exception);
    }
link | flag  | Reply

 @lennybacon
222
Wednesday 08, 2010 12:53 AM

0

Nice thanks! Don't know why these were overlooked in the framework.

link | flag  | Reply

 mikebridge
12
Wednesday 24, 2010 7:44 PM


You must log in before you can post a comment

Squeed
Made by: Fredrik Normén 2010