/// <summary>
        /// Clears the validation messages and exceptions associated with a model property,
        /// making the value in that property valid.
        /// </summary>
        /// <param name="modelState">ModelState object from which the model property will be removed, and thus be considered as valid.</param>
        /// <param name="expression">Expression tree that goes to the property that should be made valid.</param>
        public static void ClearPropertyErrors(this ModelStateDictionary modelState, Expression <Func <object> > expression)
        {
            var propertyInfo = MemberExpressionHelper.GetPropertyInfo(expression);

            if (modelState.ContainsKey(propertyInfo.Name))
            {
                modelState[propertyInfo.Name].Errors.Clear();
            }
        }
        public static string GetRegularExpressionPattern <TModel>(Expression <Func <TModel, Object> > expression)
        {
            var propertyInfo = MemberExpressionHelper.GetPropertyInfo(expression);
            var regularExpressionAttribute = propertyInfo.GetCustomAttributes(typeof(RegularExpressionAttribute), true).Cast <RegularExpressionAttribute>().FirstOrDefault();

            if (regularExpressionAttribute == null)
            {
                throw new Exception("Expression member does not have a RegularExpressionAttribute");
            }
            return(regularExpressionAttribute.Pattern);
        }
        public static string GetDisplayName <TModel>(Expression <Func <TModel, Object> > expression)
        {
            var propertyInfo     = MemberExpressionHelper.GetPropertyInfo(expression);
            var displayAttribute = propertyInfo.GetCustomAttributes(typeof(DisplayAttribute), true).Cast <DisplayAttribute>().FirstOrDefault();

            if (displayAttribute == null)
            {
                return(propertyInfo.Name);
            }
            return(displayAttribute.Name);
        }
        /// <summary>
        /// Returns the ModelState object associated with a model property.
        /// </summary>
        /// <param name="modelState">ModelStateDictionary that contains validation messages of the model.</param>
        /// <param name="expression">Expression tree that goes to the property of the model, for which to return the associated ModelState.</param>
        /// <returns>
        /// Returns the ModelState associated with the property represented by the expression tree.
        /// If there is no ModelState associated, then returns null.
        /// </returns>
        public static ModelErrorCollection GetPropertyErrors(this ModelStateDictionary modelState, Expression <Func <object> > expression)
        {
            var propertyInfo = MemberExpressionHelper.GetPropertyInfo(expression);
            var result       = modelState[propertyInfo.Name];

            if (result == null)
            {
                return(null);
            }

            return(result.Errors);
        }
        /// <summary>
        /// Add a new validation message to the collection of messages and exceptions associated with the given model property.
        /// </summary>
        /// <param name="modelState">ModelState objeto to add the validation message to.</param>
        /// <param name="expression">Expression tree that goes to the property that is not valid.</param>
        /// <param name="errorMessage">Validation message to associate with the property.</param>
        public static void AddModelError(this ModelStateDictionary modelState, Expression <Func <object> > expression, [Localizable(true)] string errorMessage)
        {
            // todo: this method should accept a resource name, instead of an error message.

            var propertyInfo = MemberExpressionHelper.GetPropertyInfo(expression);

            if (errorMessage.Contains("{0}"))
            {
                var displayAttribute = propertyInfo.GetCustomAttributes(typeof(DisplayAttribute), true).Cast <DisplayAttribute>().FirstOrDefault();
                var propertyDisplay  = displayAttribute != null ? displayAttribute.Name : propertyInfo.Name;
                errorMessage = string.Format(errorMessage, propertyDisplay);
            }

            modelState.AddModelError(propertyInfo.Name, errorMessage);
        }
        public static void AddModelError <TModel>(
            this ModelStateDictionary modelState,
            Expression <Func <TModel, object> > expression,
            string errorMessage)
        {
            // todo: this method should accept a resource name, instead of an error message.

            var propertyInfo = MemberExpressionHelper.GetPropertyInfo(expression);

            var propertyDisplay = MemberExpressionHelper.GetPropertyDisplayName(propertyInfo);

            errorMessage = string.Format(errorMessage, propertyDisplay);

            modelState.AddModelError(propertyInfo.Name, errorMessage);
        }
Esempio n. 7
0
        public static MvcHtmlString EnumDisplayFor <TModel, TValue>(this HtmlHelper <TModel> html, Expression <Func <TModel, TValue> > expression)
            where TModel : class
        {
            var propertyInfo = MemberExpressionHelper.GetPropertyInfo(expression);

            Type enumType;

            if (propertyInfo.PropertyType.IsEnum)
            {
                enumType = propertyInfo.PropertyType;
            }
            else
            {
                var attributes = propertyInfo.GetCustomAttributes(typeof(EnumDataTypeAttribute), true);
                if (attributes.Length == 0)
                {
                    throw new Exception("cannot resolve enum type");
                }

                enumType = ((EnumDataTypeAttribute)attributes[0]).EnumType;
                if (enumType == null)
                {
                    throw new Exception("cannot resolve enum type");
                }
            }

            var model = html.ViewData.Model;

            if (model == null)
            {
                return(new MvcHtmlString(""));
            }

            var modelValue = propertyInfo.GetValue(model, null);

            if (modelValue == null)
            {
                return(new MvcHtmlString(""));
            }

            return(new MvcHtmlString(EnumHelper.GetText((int)modelValue, enumType)));
        }
        /// <summary>
        /// Remove the validation messages and exceptions associated with a model property,
        /// making the value in that property valid.
        /// </summary>
        /// <param name="modelState">ModelState object from which the model property will be removed, and thus be considered as valid.</param>
        /// <param name="expression">Expression tree that goes to the property that should be made valid.</param>
        /// <param name="hasPrefix">Whether the model-state entry name has a prefix or not.</param>
        public static void Remove(this ModelStateDictionary modelState, Expression <Func <object> > expression, bool hasPrefix = false)
        {
            var propertyInfo = MemberExpressionHelper.GetPropertyInfo(expression);

            if (hasPrefix)
            {
                var name = '.' + propertyInfo.Name;
                foreach (var modelStateItem in modelState.ToArray())
                {
                    if (modelStateItem.Key.EndsWith(name))
                    {
                        modelState.Remove(modelStateItem.Key);
                    }
                }
            }
            else
            {
                modelState.Remove(propertyInfo.Name);
            }
        }