コード例 #1
0
        private string GetCultureCode(ActionContext context)
        {
            IValueProvider provider = Configuration.Instance.Models
                                      .ValueProviders.GetProvider(context.Context,
                                                                  ParameterSource.FromString(Source));

            ValueProviderResult result = provider.GetValue(_parameterName);

            return((result == null) ? null : result.GetValue <string>());
        }
コード例 #2
0
        protected ICollection <T> ReadCollection(BindingContext context)
        {
            Type elementType = typeof(T);

            SortedDictionary <int, T> convertedValues = new SortedDictionary <int, T>();
            ValueProviderResult       result          = context.GetValue();
            object value = (result == null) ? null : result.GetValue <string>();

            if (value != null && IsSimpleType)
            {
                string[] split = ((string)value).Split(new char[] { ',' },
                                                       StringSplitOptions.RemoveEmptyEntries);

                for (int i = 0; i < split.Length; ++i)
                {
                    string elementKey = CreateSubMemberName(context.ModelName, i.ToString());

                    ValueDictionary bindingData = new ValueDictionary();
                    bindingData.Add(elementKey, split[i]);

                    BindingContext inner = new BindingContext(context, elementType,
                                                              elementKey, new DictionaryValueProvider(bindingData), context.ModelState);

                    value = ElementBinder.Bind(inner);
                    convertedValues[i] = ValidateElement(context, elementKey, value);
                }
            }
            else
            {
                foreach (string kvp in context.ValueProvider.Keys)
                {
                    int?index = GetItemIndex(kvp, context.ModelName);
                    if (!index.HasValue)
                    {
                        continue;
                    }

                    if (convertedValues.ContainsKey(index.Value))
                    {
                        continue;
                    }

                    string         elementKey = CreateSubMemberName(context.ModelName, index.ToString());
                    BindingContext inner      = new BindingContext(context, elementType,
                                                                   elementKey, context.ValueProvider, context.ModelState);

                    value = ElementBinder.Bind(inner);
                    convertedValues[index.Value] = ValidateElement(context, elementKey, value);
                }
            }
            return(convertedValues.Values);
        }
コード例 #3
0
        public object Bind(BindingContext context)
        {
            Precondition.Require(context, () => Error.ArgumentNull("context"));

            ValueProviderResult result = context.ValueProvider.GetValue(context.ModelName);

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

            return(result.GetValue <HttpPostedFileBase>());
        }
コード例 #4
0
        public static TValue GetValue <TValue>(this IValueProvider provider,
                                               string name, TValue defaultValue, CultureInfo culture)
        {
            Precondition.Require(provider, () => Error.ArgumentNull("provider"));
            ValueProviderResult result = provider.GetValue(name);

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

            result.Culture = culture;
            return(result.GetValue <TValue>(defaultValue));
        }
コード例 #5
0
        public object Bind(BindingContext context)
        {
            Precondition.Require(context, () => Error.ArgumentNull("context"));
            ValueProviderResult result = context.ValueProvider.GetValue(context.ModelName);

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

            List <HttpPostedFileBase> files = result
                                              .GetValue <IEnumerable <HttpPostedFileBase> >().ToList();

            Type type = context.ModelType;

            if (type == typeof(IEnumerable <HttpPostedFileBase>) || type == typeof(HttpPostedFileBase[]))
            {
                return(files.ToArray());
            }

            if (type == typeof(ICollection <HttpPostedFileBase>) || type == typeof(Collection <HttpPostedFileBase>))
            {
                return(new Collection <HttpPostedFileBase>(files));
            }

            if (type == typeof(IList <HttpPostedFileBase>) || type == typeof(List <HttpPostedFileBase>))
            {
                return(files);
            }

            ConstructorInfo constructor = type.GetConstructor(new Type[] { typeof(IEnumerable <HttpPostedFileBase>) });

            if (constructor == null)
            {
                throw Error.UnsupportedModelType(type);
            }

            return(constructor.CreateInvoker().Invoke(files));
        }