Example #1
0
        private T ExtractEnum <T>(ControllerContext controllerContext, ModelBindingContext bindingContext, string pattern)
            where T : struct
        {
            if (!typeof(T).IsEnum)
            {
                throw new ArgumentException("Expected an enumeration.");
            }
            var keys =
                controllerContext.HttpContext.Request.Form.AllKeys.Where(
                    k => System.Text.RegularExpressions.Regex.IsMatch(k, pattern + "\\[.+\\]"));
            var values = new List <T>();

            foreach (var key in keys)
            {
                values.Add((T)Enum.Parse(typeof(T), controllerContext.HttpContext.Request.Form[key]));
            }
            bindingContext.ModelState.Remove(pattern);
            return(EnumHelpers.Combine(values));
        }