示例#1
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);
        }