/// <summary>
        /// Binds the model value to an entity by using the specified controller context and binding context.
        /// </summary>
        /// <returns>
        /// The bound value.
        /// </returns>
        /// <param name = "controllerContext">The controller context.</param>
        /// <param name = "bindingContext">The binding context.</param>
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var modelType = bindingContext.ModelType;

            // Will look for the entity Id either named "ModelName" or "ModelName.Id"
            var valueProviderResult =
                bindingContext.ValueProvider.GetValue(bindingContext.ModelName) ??
                bindingContext.ValueProvider.GetValue(bindingContext.ModelName + ".Id");

            if (valueProviderResult == null)
            {
                return(base.BindModel(controllerContext, bindingContext));
            }

            string rawId = (valueProviderResult.RawValue as string[]).First();

            if (string.IsNullOrEmpty(rawId))
            {
                return(null);
            }

            try {
                var typedId = Convert.ChangeType(rawId, typeof(int));
                return(EntityRetriever.GetEntityFor(modelType, typedId, typeof(int)));
            }
            catch (Exception) {
                // If the Id conversion failed for any reason, just return null
                return(null);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Binds the model to a value by using the specified controller context and binding context.
        /// </summary>
        /// <returns>
        /// The bound value.
        /// </returns>
        /// <param name = "controllerContext">The controller context.</param>
        /// <param name = "bindingContext">The binding context.</param>
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var collectionType       = bindingContext.ModelType;
            var collectionEntityType = collectionType.GetGenericArguments().First();

            var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

            if (valueProviderResult == null)
            {
                return(base.BindModel(controllerContext, bindingContext));
            }

            var rawValue = valueProviderResult.RawValue as string[];

            var countOfEntityIds = rawValue.Length;
            var entities         = Array.CreateInstance(collectionEntityType, countOfEntityIds);

            for (var i = 0; i < countOfEntityIds; i++)
            {
                string rawId = rawValue[i];

                if (string.IsNullOrEmpty(rawId))
                {
                    return(null);
                }

                object typedId = Convert.ChangeType(rawId, typeof(int));
                object entity  = EntityRetriever.GetEntityFor(collectionEntityType, typedId, typeof(int));
                entities.SetValue(entity, i);
            }

            return(entities);
        }