コード例 #1
0
        /// <summary>
        /// Bind a <see cref="DynamicViewModel" />.
        /// </summary>
        /// <param name="controllerContext">The context within which the controller operates. The context information includes the controller, HTTP content, request context, and route data.</param>
        /// <param name="bindingContext">The context within which the model is bound. The context includes information such as the model object, model name, model type, property filter, and value provider.</param>
        /// <returns>A <see cref="DynamicViewModel" /> binded with its corresponding values from the binding context value provider.</returns>
        public object BindDynamicViewModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
        #if DEBUG
            var step = MiniProfiler.Current.Step("InfrastructureModelBinder.BindDynamicViewModel");

            try
            {
#endif


            byte[] serializedModel = new ByteArrayModelBinder().BindModel(controllerContext, bindingContext) as byte[];

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

            // Deserialize to retrieve dynamic view model without user supplied values
            var model = serializedModel.Deserialize() as DynamicViewModel;

            int count        = 0;
            string indexName = CreateSubIndexName(bindingContext.ModelName, count);

            if (model != null)
            {
                var updateIndex = new Dictionary <int, object>();

                int valueIndex = 0;
                foreach (var value in model.Values)
                {
                    if (value is LabelViewModel)
                    {
                        valueIndex++;
                        continue;
                    }

                    var properties = TypeDescriptor.GetProperties(value).Cast <PropertyDescriptor>();

                    // Loop through each property in the current row
                    foreach (var property in properties)
                    {
                        if (property.Name != "Value")
                        {
                            continue;
                        }

                        var propertyName = string.Format("{0}.Value", indexName);

                        // Prepare new binding context for current property
                        var propertyBindingContext = new ModelBindingContext
                        {
                            ModelMetadata  = System.Web.Mvc.ModelMetadataProviders.Current.GetMetadataForProperty(() => value, value.GetType(), property.Name),
                            ModelName      = propertyName,
                            ModelState     = bindingContext.ModelState,
                            PropertyFilter = bindingContext.PropertyFilter,
                            ValueProvider  = bindingContext.ValueProvider
                        };

                        // Get binded value
                        var bindedPropertyValue = BindModel(controllerContext, propertyBindingContext);
                        var bindable            = propertyBindingContext.ModelMetadata.GetAttribute <BindableAttribute>();

                        // Only bind if bindable
                        if (bindable != null && bindable.IsBindable())
                        {
                            property.SetValue(value, bindedPropertyValue);

                            updateIndex.Add(valueIndex, value);
                        }

                        // Get next index name
                        indexName = CreateSubIndexName(bindingContext.ModelName, ++count);
                    }

                    valueIndex++;
                }

                foreach (var update in updateIndex)
                {
                    model.Values[update.Key] = update.Value;
                }
            }

            return(model);


#if DEBUG
        }

        finally
        {
            if (step != null)
            {
                step.Dispose();
            }
        }
#endif
        }
コード例 #2
0
        /// <summary>Binds the model by using the specified controller context and binding context.</summary>
        /// <param name="controllerContext">The context within which the controller operates. The context information includes the controller, HTTP content, request context, and route data.</param>
        /// <param name="bindingContext">The context within which the model is bound. The context includes information such as the model object, model name, model type, property filter, and value provider.</param>
        /// <returns>The bound object.</returns>
        /// <exception cref="T:System.ArgumentNullException">The <paramref name="bindingContext" />parameter is null.</exception>
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
      #if DEBUG
            var step = MiniProfiler.Current.Step("InfrastructureModelBinder.BindModel");

            try
            {
#endif
            if (bindingContext == null)
            {
                throw new ArgumentNullException("bindingContext");
            }

            var serialized = bindingContext.ModelMetadata.GetAttribute <SerializedAttribute>();

            if (serialized != null)
            {
                byte[] model = new ByteArrayModelBinder().BindModel(controllerContext, bindingContext) as byte[];

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

                return(model.Deserialize());
            }

            // Allow binding for currency values
            if (bindingContext.ModelMetadata.DataTypeName == DataType.Currency.ToString())
            {
                string attemptedValue = bindingContext.ValueProvider.GetValue(bindingContext.ModelName).AttemptedValue;

                if (bindingContext.ModelType == typeof(double) || bindingContext.ModelType == typeof(double?))
                {
                    double d;
                    double.TryParse(attemptedValue, NumberStyles.Any, CultureInfo.CurrentCulture, out d);
                    return(d);
                }

                if (bindingContext.ModelType == typeof(decimal) || bindingContext.ModelType == typeof(decimal?))
                {
                    decimal d;
                    decimal.TryParse(attemptedValue, NumberStyles.Any, CultureInfo.CurrentCulture, out d);
                    return(d);
                }

                if (bindingContext.ModelType == typeof(float) || bindingContext.ModelType == typeof(float?))
                {
                    float f;
                    float.TryParse(attemptedValue, NumberStyles.Any, CultureInfo.CurrentCulture, out f);
                    return(f);
                }
            }

            if (bindingContext.ModelType == typeof(SelectList))
            {
                return(BindSelectList(controllerContext, bindingContext));
            }

            if (bindingContext.ModelType == typeof(MultiSelectList))
            {
                return(BindMultiSelectList(controllerContext, bindingContext));
            }

            if (bindingContext.ModelType == typeof(IEnumerable <SelectListItem>))
            {
                return(BindEnumerableSelectListItem(controllerContext, bindingContext));
            }

            if (bindingContext.ModelType == typeof(IEnumerable <string>))
            {
                return(BindEnumerableString(controllerContext, bindingContext));
            }

            if (bindingContext.ModelType.IsGenericType && new[] { typeof(IPageable <>), typeof(Pageable <>) }.Contains(bindingContext.ModelType.GetGenericTypeDefinition()))
            {
                return(BindPageable(controllerContext, bindingContext));
            }

            if (bindingContext.ModelMetadata.DataTypeName == CustomDataType.Grid || bindingContext.ModelMetadata.DataTypeName == CustomDataType.GridEditable)
            {
                return(BindGrid(controllerContext, bindingContext));
            }

            if (bindingContext.ModelType == typeof(DynamicViewModel))
            {
                return(BindDynamicViewModel(controllerContext, bindingContext));
            }

            if (bindingContext.ModelType == typeof(AddressViewModel))
            {
                return(BindAddressViewModel(controllerContext, bindingContext));
            }

            if (bindingContext.ModelType == typeof(JobseekerSearchViewModel))
            {
                return(BindJobSeekerSearchViewModel(controllerContext, bindingContext));
            }

            if (bindingContext.ModelType == typeof(DateTime) || bindingContext.ModelType == typeof(DateTime?))
            {
                return(BindDateTime(controllerContext, bindingContext));
            }

            return(base.BindModel(controllerContext, bindingContext));

#if DEBUG
        }

        finally
        {
            if (step != null)
            {
                step.Dispose();
            }
        }
#endif
        }