Exemple #1
0
        /// <summary> Parse Data values. </summary>
        private Dictionary <string, string> ParseData(ModelBindingContext bindingContext, DictionaryValueProvider <object> provider, string prefix)
        {
            Dictionary <string, string> data = new Dictionary <string, string>();

            IDictionary <string, string> keys = provider.GetKeysFromPrefix(prefix);

            foreach (string shortKey in keys.Keys)
            {
                string key = prefix + "." + shortKey;

                string value = GetValue(bindingContext, key);

                data[shortKey] = value;
            }

            return(data);
        }
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            string prefix = "__metadata";
            DictionaryValueProvider <object> provider = GetDictionaryValueProvider(bindingContext);
            IDictionary <string, string>     keys     = provider.GetKeysFromPrefix(prefix);

            Metadata metadata = new Metadata();

            foreach (string shortKey in keys.Keys)
            {
                string key = prefix + "." + shortKey;

                string value = GetValue(bindingContext, key);

                metadata[shortKey] = value;
            }

            return(metadata);
        }
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            //DictionaryValueProvider<object> provider = this.GetDictionaryValueProvider(bindingContext);
            //ValueProviderResult value = provider.GetValue(bindingContext.ModelName);

            object provider = this.FindValue(bindingContext);

            // Only proceed if value is indeed stringified
            if (provider == null)
            {
                return(null);
            }

            if (provider is ValueProviderResult)
            {
                ValueProviderResult result = (ValueProviderResult)provider;

                return(this.ParseFrom(bindingContext, result));
            }
            else if (provider is DictionaryValueProvider <object> )
            {
                // Only JObject from here
                if (bindingContext.ModelType != typeof(JObject))
                {
                    if (this.ModelType != null)
                    {
                        if (bindingContext.ModelType != this.ModelType)
                        {
                            return(null);
                        }
                    }
                    else
                    {
                        return(null);
                    }
                }

                DictionaryValueProvider <object> dvp = (DictionaryValueProvider <object>)provider;

                IDictionary <string, string> keys = dvp.GetKeysFromPrefix(bindingContext.ModelName);
                if (keys.Count == 0)
                {
                    return(null);
                }

                // TODO: build recursively (now supports only single layer
                JObject jObj = new JObject();

                foreach (KeyValuePair <string, string> pair in keys)
                {
                    //string name = pair.Key;
                    //string value = pair.Value;

                    //ValueProviderResult vpr1 = dvp.GetValue(name);
                    ValueProviderResult value = dvp.GetValue(pair.Value);

                    if (value != null)
                    {
                        //    string attempted = value.AttemptedValue;
                        //    object rawValue = value.RawValue;

                        jObj[pair.Key] = value.RawValue != null?JToken.FromObject(value.RawValue) : null;
                    }

                    //if (dvp.ContainsPrefix(value))
                    //{
                    //    IDictionary<string, string> keys2 = dvp.GetKeysFromPrefix(value);
                    //}
                }

                return(jObj);
            }

            return(null);
        }