Ejemplo n.º 1
0
        /// <summary>
        /// Checks to see whether the provided value falls within the range or not.
        /// </summary>
        /// <param name="variable">The variable in context.</param>
        /// <param name="getVariableName">The name of the variable that should be populated in the error context.</param>
        /// <param name="minimum">The minimum value allowed in the range.</param>
        /// <param name="maximum">The maximum value allowed in the range.</param>
        /// <param name="actionContext">Current action context.</param>
        /// <param name="modelState">Current model object.</param>
        /// <returns>The status of the operation.</returns>
        public static bool Range <T>(T variable, Expression <Func <T, T> > getVariableName, T minimum, T maximum, HttpActionContext actionContext, ModelStateDictionary modelState)
        {
            var variableName = ((MemberExpression)getVariableName.Body).Member.Name;
            var attr         = new RangeAttribute(Convert.ToDouble(minimum), Convert.ToDouble(maximum));

            if (!attr.IsValid(variable))
            {
                modelState.AddModelError(attr.ToString(), attr.FormatErrorMessage(variableName));

                AddErrorToResponse(actionContext, modelState);
                return(false);
            }

            return(true);
        }