/// <summary>
        /// Bind the model
        /// </summary>
        /// <param name="context">Context information</param>
        /// <returns>
        /// An object of the specified type (<seealso cref="IModelBinderContext.ModelType)" />
        /// </returns>
        /// <exception cref="System.FormatException"></exception>
        public object Bind(IModelBinderContext context)
        {
            if (context == null) throw new ArgumentNullException("context");

            var fieldName = context.Prefix + context.ModelName + "[";
            if (string.IsNullOrEmpty(fieldName))
                throw new ModelBindingException("Either IModelBinderContext.ModelName or IModelBinderContext.Prefix has to be specified");

            if (context.ModelType == null)
                throw new ModelBindingException("IModelBinderContext.ModelType has to be specified.");

            var arrayType = context.ModelType.GetElementType();
            if (arrayType == null)
                throw new ModelBindingException("Failed to find the element type.", context.ModelType.FullName, null);

            var singleField = context.ValueProvider.Get(fieldName + "]");
            if (singleField != null)
            {
                var array = Array.CreateInstance(arrayType, singleField.Count);
                for (var i = 0; i < array.Length; i++)
                {
                    array.SetValue(Convert.ChangeType(singleField[i], arrayType), i);
                }
                return array;
            }

            return BuildIndexedArray(context, fieldName, arrayType);
        }
        /// <summary>
        /// Bind the model
        /// </summary>
        /// <param name="context">Context information</param>
        /// <returns>
        /// An object of the specified type (<seealso cref="IModelBinderContext.ModelType)" />
        /// </returns>
        public object Bind(IModelBinderContext context)
        {
            var method    = GetType().GetMethod("BindGeneric", BindingFlags.Instance | BindingFlags.NonPublic);
            var genMethod = method.MakeGenericMethod(context.ModelType.GetGenericArguments()[1]);

            return(genMethod.Invoke(this, new object[] { context }));
        }
        /// <summary>
        /// Bind the model
        /// </summary>
        /// <param name="context">Context information</param>
        /// <returns>
        /// An object of the specified type (<seealso cref="IModelBinderContext.ModelType)" />
        /// </returns>
        public object Bind(IModelBinderContext context)
        {
            var name = string.IsNullOrEmpty(context.Prefix)
                           ? context.ModelName
                           : context.Prefix + "." + context.ModelName;
            var parameter = context.ValueProvider.Get(name);

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

            object value = parameter.Value;

            if (!context.ModelType.IsAssignableFrom(typeof(string)))
            {
                try
                {
                    value = Convert.ChangeType(value, context.ModelType);
                }
                catch (Exception err)
                {
                    throw new ModelBindingException(err.Message, context.ModelName, value);
                }
            }

            return(value);
        }
示例#4
0
        /// <summary>
        /// Bind the model
        /// </summary>
        /// <param name="context">Context information</param>
        /// <returns>
        /// An object of the specified type (<seealso cref="IModelBinderContext.ModelType)" />
        /// </returns>
        public object Bind(IModelBinderContext context)
        {
            if (context.ModelType.GetConstructor(new Type[0]) == null)
            {
                throw new ModelBindingException("Do not have a default constructor.", context.ModelName, context.ModelType);
            }

            //var model = Activator.CreateInstance(context.ModelType);
            var model  = ServiceResolver.Current.Resolve(context.ModelType);
            var prefix = StringUtility.IsNullOrEmpty(context.Prefix) ? context.ModelName : context.Prefix + "." + context.ModelName;

            //foreach (Type property in TypeUtility.GetProperties(context.ModelType))
            //{

            //    if (!property.CanWrite)
            //        continue;

            //    var value = context.Execute(property.PropertyType, prefix, property.Name);

            //    property.SetValue(model, value, null);

            //}

            return(model);
        }
        private object BindGeneric <TValueType>(IModelBinderContext context)
        {
            var fieldName = context.Prefix + context.ModelName + "[";
            var fields    = new List <string>();

            foreach (var item in context.ValueProvider.Find(fieldName))
            {
                var pos = item.Name.IndexOf(']', fieldName.Length);
                if (pos == -1)
                {
                    throw new FormatException("Expected to find ']' to mark end of array in " + item.Name);
                }

                var name = item.Name.Substring(0, pos + 1);
                if (!fields.Contains(name))
                {
                    fields.Add(name);
                }
            }
            if (!fields.Any())
            {
                return(null);
            }

            var model = new Dictionary <string, TValueType>();

            for (var i = 0; i < fields.Count; i++)
            {
                var value   = (TValueType)context.Execute(typeof(TValueType), context.Prefix, fields[i]);
                var pos     = fields[i].IndexOf('[');
                var indexer = fields[i].Substring(pos + 1).TrimEnd(']').Trim('\'');
                model.Add(indexer, value);
            }
            return(model);
        }
示例#6
0
        /// <summary>
        /// Bind the model
        /// </summary>
        /// <param name="context">Context information</param>
        /// <returns>
        /// An object of the specified type (<seealso cref="IModelBinderContext.ModelType)" />
        /// </returns>
        public object Bind(IModelBinderContext context)
        {
            if (context.ModelType.GetConstructor(new Type[0]) == null)
            {
                throw new ModelBindingException("Do not have a default constructor.", context.ModelName, context.ModelType);
            }

            var model  = Activator.CreateInstance(context.ModelType);
            var prefix = string.IsNullOrEmpty(context.Prefix) ? context.ModelName : context.Prefix + "." + context.ModelName;

            foreach (var property in context.ModelType.GetProperties())
            {
                if (!property.CanWrite)
                {
                    continue;
                }

                var value = context.Execute(property.PropertyType, prefix, property.Name);


                property.SetValue(model, value, null);
            }

            return(model);
        }
示例#7
0
 /// <summary>
 /// Determines whether this instance can bind the specified model.
 /// </summary>
 /// <param name="context">Context infromation.</param>
 /// <returns>
 ///   <c>true</c> if this instance can handle the model; otherwise <c>false</c>.
 /// </returns>
 public bool CanBind(IModelBinderContext context)
 {
     if (context == null)
     {
         throw new ArgumentNullException("context");
     }
     return(context.ModelType.IsArray);
 }
        /// <summary>
        /// Bind the model
        /// </summary>
        /// <param name="context">Context information</param>
        /// <returns>
        /// An object of the specified type (<seealso cref="IModelBinderContext.ModelType)" />
        /// </returns>
        public object Bind(IModelBinderContext context)
        {
            var parameter = context.ValueProvider.Get(context.Prefix + context.ModelName);
            if (parameter == null || string.IsNullOrEmpty(parameter.Value))
                return 0;


            return char.IsDigit(parameter.Value[0])
                       ? int.Parse(parameter.Value)
                       : Enum.Parse(context.ModelType, parameter.Value, true);
        }
示例#9
0
        object IModelBinder.Bind(IModelBinderContext context)
        {
            foreach (IModelBinder modelBinder in _binders)
            {
                if (modelBinder.CanBind(context))
                {
                    return(modelBinder.Bind(context));
                }
            }

            return(context.ModelType.IsClass ? null : ServiceResolver.Current.Resolve(context.ModelType)); //Activator.CreateInstance(context.ModelType);
        }
示例#10
0
        object IModelBinder.Bind(IModelBinderContext context)
        {
            foreach (var modelBinder in _binders)
            {
                if (modelBinder.CanBind(context))
                {
                    return(modelBinder.Bind(context));
                }
            }

            return(context.ModelType.IsClass ? null : Activator.CreateInstance(context.ModelType));
        }
示例#11
0
        object IModelBinder.Bind(IModelBinderContext context)
        {
            foreach (IModelBinder modelBinder in _binders)
            {
                if (modelBinder.CanBind(context))
                {
                    return modelBinder.Bind(context);
                }
            }

            return context.ModelType.IsClass ? null : ServiceResolver.Current.Resolve(context.ModelType); //Activator.CreateInstance(context.ModelType);
        }
示例#12
0
        /// <summary>
        /// Bind the model
        /// </summary>
        /// <param name="context">Context information</param>
        /// <returns>
        /// An object of the specified type (<seealso cref="IModelBinderContext.ModelType)" />
        /// </returns>
        public object Bind(IModelBinderContext context)
        {
            var parameter = context.ValueProvider.Get(context.Prefix + context.ModelName);

            if (parameter == null || string.IsNullOrEmpty(parameter.Value))
            {
                return(0);
            }


            return(char.IsDigit(parameter.Value[0])
                       ? int.Parse(parameter.Value)
                       : Enum.Parse(context.ModelType, parameter.Value, true));
        }
示例#13
0
 /// <summary>
 /// Determines whether this instance can bind the specified model.
 /// </summary>
 /// <param name="context">Context information.</param>
 /// <returns>
 ///   <c>true</c> if this instance can handle the model; otherwise <c>false</c>.
 /// </returns>
 public bool CanBind(IModelBinderContext context)
 {
     return(context.ModelType == typeof(String) ||
            context.ModelType == typeof(Boolean) ||
            context.ModelType == typeof(Byte) ||
            context.ModelType == typeof(SByte) ||
            context.ModelType == typeof(Int16) ||
            context.ModelType == typeof(UInt16) ||
            context.ModelType == typeof(Int32) ||
            context.ModelType == typeof(UInt32) ||
            context.ModelType == typeof(Int64) ||
            context.ModelType == typeof(UInt64) ||
            context.ModelType == typeof(Double));
 }
        /// <summary>
        /// Determines whether this instance can bind the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns>
        ///   <c>true</c> if this instance is an dictionary with a string key; otherwise <c>false</c>.
        /// </returns>
        public bool CanBind(IModelBinderContext context)
        {
            if (context.ModelType.IsGenericType)
            {
                var modelType = context.ModelType.GetGenericTypeDefinition();
                if (context.ModelType.GetGenericArguments()[0] != typeof(string))
                {
                    return(false);
                }

                return(modelType.IsClass
                           ? typeof(Dictionary <,>).IsAssignableFrom(modelType)
                           : typeof(IDictionary <,>).IsAssignableFrom(modelType));
            }

            return(false);
        }
示例#15
0
        private static object BuildIndexedArray(IModelBinderContext context, string fieldName, Type arrayType)
        {
            var fields  = new List <string>();
            var indexes = new List <int>();

            foreach (var item in context.ValueProvider.Find(fieldName))
            {
                var pos = item.Name.IndexOf(']', fieldName.Length);
                if (pos == -1)
                {
                    throw new ModelBindingException("Expected to find ']' to mark end of array.", fieldName + "]", item.Name);
                }

                var name = item.Name.Substring(0, pos + 1);
                if (!fields.Contains(name))
                {
                    fields.Add(name);

                    var index = ExtractIndex(name, fieldName);
                    indexes.Add(index);
                }
            }

            if (!fields.Any())
            {
                return(null);
            }

            ValidateIndexes(indexes, fieldName);

            var array = Array.CreateInstance(arrayType, fields.Count);

            for (var i = 0; i < fields.Count; i++)
            {
                var index = indexes[i];

                //prefix already includes the field
                var value = context.Execute(arrayType, "", fields[i]);
                array.SetValue(value, index);
            }

            return(array);
        }
示例#16
0
        /// <summary>
        /// Bind the model
        /// </summary>
        /// <param name="context">Context information</param>
        /// <returns>
        /// An object of the specified type (<seealso cref="IModelBinderContext.ModelType)" />
        /// </returns>
        public object Bind(IModelBinderContext context)
        {
            if (context.ModelType.GetConstructor(new Type[0]) == null)
                throw new ModelBindingException("Do not have a default constructor.", context.ModelName, context.ModelType);

            var model = Activator.CreateInstance(context.ModelType);
            var prefix = string.IsNullOrEmpty(context.Prefix) ? context.ModelName : context.Prefix + "." + context.ModelName;
            foreach (var property in context.ModelType.GetProperties())
            {
                if (!property.CanWrite)
                    continue;

                var value = context.Execute(property.PropertyType, prefix, property.Name);

                
                property.SetValue(model, value, null);

            }

            return model;
        }
示例#17
0
        /// <summary>
        /// Bind the model
        /// </summary>
        /// <param name="context">Context information</param>
        /// <returns>
        /// An object of the specified type (<seealso cref="IModelBinderContext.ModelType)" />
        /// </returns>
        /// <exception cref="System.FormatException"></exception>
        public object Bind(IModelBinderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            var fieldName = context.Prefix + context.ModelName + "[";

            if (string.IsNullOrEmpty(fieldName))
            {
                throw new ModelBindingException("Either IModelBinderContext.ModelName or IModelBinderContext.Prefix has to be specified");
            }

            if (context.ModelType == null)
            {
                throw new ModelBindingException("IModelBinderContext.ModelType has to be specified.");
            }

            var arrayType = context.ModelType.GetElementType();

            if (arrayType == null)
            {
                throw new ModelBindingException("Failed to find the element type.", context.ModelType.FullName, null);
            }

            var singleField = context.ValueProvider.Get(fieldName + "]");

            if (singleField != null)
            {
                var array = Array.CreateInstance(arrayType, singleField.Count);
                for (var i = 0; i < array.Length; i++)
                {
                    array.SetValue(Convert.ChangeType(singleField[i], arrayType), i);
                }
                return(array);
            }

            return(BuildIndexedArray(context, fieldName, arrayType));
        }
示例#18
0
        /// <summary>
        /// Bind the model
        /// </summary>
        /// <param name="context">Context information</param>
        /// <returns>
        /// An object of the specified type (<seealso cref="IModelBinderContext.ModelType)" />
        /// </returns>
        public object Bind(IModelBinderContext context)
        {
            if (context.ModelType.GetConstructor(new Type[0]) == null)
                throw new ModelBindingException("Do not have a default constructor.", context.ModelName, context.ModelType);

            //var model = Activator.CreateInstance(context.ModelType);
            var model = ServiceResolver.Current.Resolve(context.ModelType);
            var prefix = StringUtility.IsNullOrEmpty(context.Prefix) ? context.ModelName : context.Prefix + "." + context.ModelName;

            //foreach (Type property in TypeUtility.GetProperties(context.ModelType))
            //{

            //    if (!property.CanWrite)
            //        continue;

            //    var value = context.Execute(property.PropertyType, prefix, property.Name);

            //    property.SetValue(model, value, null);

            //}

            return model;
        }
示例#19
0
        public bool CanBind(IModelBinderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var modelType = context.ModelType;

            return(modelType == typeof(string) ||
                   modelType == typeof(bool) ||
                   modelType == typeof(byte) ||
                   modelType == typeof(byte[]) ||
                   modelType == typeof(sbyte) ||
                   modelType == typeof(sbyte[]) ||
                   modelType == typeof(short) ||
                   modelType == typeof(ushort) ||
                   modelType == typeof(int) ||
                   modelType == typeof(uint) ||
                   modelType == typeof(long) ||
                   modelType == typeof(ulong) ||
                   modelType == typeof(double));
        }
        /// <summary>
        /// Bind the model
        /// </summary>
        /// <param name="context">Context information</param>
        /// <returns>
        /// An object of the specified type (<seealso cref="IModelBinderContext.ModelType)" />
        /// </returns>
        public object Bind(IModelBinderContext context)
        {
            var name = string.IsNullOrEmpty(context.Prefix)
                           ? context.ModelName
                           : context.Prefix + "." + context.ModelName;
            var parameter = context.ValueProvider.Get(name);
            if (parameter == null)
                return null;

            object value = parameter.Value;
            if (!context.ModelType.IsAssignableFrom(typeof(string)))
            {
                try
                {
                    value = Convert.ChangeType(value, context.ModelType);
                }
                catch (Exception err)
                {
                    throw new ModelBindingException(err.Message, context.ModelName, value);
                }
            }

            return value;
        }
        private static object BuildIndexedArray(IModelBinderContext context, string fieldName, Type arrayType)
        {
            var fields = new List<string>();
            var indexes = new List<int>();

            foreach (var item in context.ValueProvider.Find(fieldName))
            {
                var pos = item.Name.IndexOf(']', fieldName.Length);
                if (pos == -1)
                    throw new ModelBindingException("Expected to find ']' to mark end of array.", fieldName + "]", item.Name);

                var name = item.Name.Substring(0, pos + 1);
                if (!fields.Contains(name))
                {
                    fields.Add(name);

                    var index = ExtractIndex(name, fieldName);
                    indexes.Add(index);
                }
            }

            if (!fields.Any())
                return null;

            ValidateIndexes(indexes, fieldName);

            var array = Array.CreateInstance(arrayType, fields.Count);
            for (var i = 0; i < fields.Count; i++)
            {
                var index = indexes[i];

                //prefix already includes the field
                var value = context.Execute(arrayType, "", fields[i]);
                array.SetValue(value, index);
            }

            return array;
        }
 /// <summary>
 /// Determines whether this instance can bind the specified model.
 /// </summary>
 /// <param name="context">Context infromation.</param>
 /// <returns>
 ///   <c>true</c> if this instance can handle the model; otherwise <c>false</c>.
 /// </returns>
 public bool CanBind(IModelBinderContext context)
 {
     return context.ModelType.IsPrimitive
            || context.ModelType == typeof (string);
 }
 /// <summary>
 /// Determines whether this instance can bind the specified model.
 /// </summary>
 /// <param name="context">Context infromation.</param>
 /// <returns>
 ///   <c>true</c> if this instance can handle the model; otherwise <c>false</c>.
 /// </returns>
 public bool CanBind(IModelBinderContext context)
 {
     if (context == null) throw new ArgumentNullException("context");
     return context.ModelType.IsArray;
 }
示例#24
0
        public object Bind(IModelBinderContext context)
        {
            var name = string.IsNullOrEmpty(context.Prefix)
                           ? context.ModelName
                           : context.Prefix + "." + context.ModelName;

            var parameter = context.ValueProvider.Get(name);

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

            object value = parameter;

            try
            {
                if (context.ModelType == typeof(string))
                {
                    return(value);
                }

                if (context.ModelType == typeof(int))
                {
                    return(value = int.Parse((string)value));
                }

                if (context.ModelType == typeof(bool))
                {
                    if ((string)value == "1" || ((string)value).ToUpper() == bool.TrueString.ToUpper())
                    {
                        return(true);
                    }
                    else if ((string)value == "0" || ((string)value).ToUpper() == bool.FalseString.ToUpper())
                    {
                        return(false);
                    }

                    return(null);
                }

                if (context.ModelType == typeof(byte))
                {
                    return(value = byte.Parse((string)value));
                }

                if (context.ModelType == typeof(byte[]))
                {
                    return(value = Encoding.UTF8.GetBytes((string)value));
                }

                if (context.ModelType == typeof(sbyte))
                {
                    return(value = sbyte.Parse((string)value));
                }

                if (context.ModelType == typeof(sbyte[]))
                {
                    return(value = Encoding.UTF8.GetBytes((string)value));
                }

                if (context.ModelType == typeof(short))
                {
                    return(value = short.Parse((string)value));
                }

                if (context.ModelType == typeof(ushort))
                {
                    return(value = ushort.Parse((string)value));
                }

                if (context.ModelType == typeof(int))
                {
                    return(value = int.Parse((string)value));
                }

                if (context.ModelType == typeof(uint))
                {
                    return(value = uint.Parse((string)value));
                }

                if (context.ModelType == typeof(long))
                {
                    return(value = long.Parse((string)value));
                }

                if (context.ModelType == typeof(ulong))
                {
                    return(value = ulong.Parse((string)value));
                }

                if (context.ModelType == typeof(double))
                {
                    return(value = double.Parse((string)value));
                }
            }
            catch (Exception ex)
            {
                throw new ModelBindingException(ex.Message, context.ModelName, value);
            }

            return(null);
        }
示例#25
0
 bool IModelBinder.CanBind(IModelBinderContext context)
 {
     return(true);
 }
示例#26
0
 /// <summary>
 /// Determines whether this instance can bind the specified model.
 /// </summary>
 /// <param name="context">Context infromation.</param>
 /// <returns>
 ///   <c>true</c> if this instance can handle the model; otherwise <c>false</c>.
 /// </returns>
 public bool CanBind(IModelBinderContext context)
 {
     return(context.ModelType.IsEnum);
 }
示例#27
0
        /// <summary>
        /// Bind the model
        /// </summary>
        /// <param name="context">Context information</param>
        /// <returns>
        /// An object of the specified type (<seealso cref="IModelBinderContext.ModelType)" />
        /// </returns>
        public object Bind(IModelBinderContext context)
        {
            var name = StringUtility.IsNullOrEmpty(context.Prefix)
                           ? context.ModelName
                           : context.Prefix + "." + context.ModelName;
            var parameter = context.ValueProvider.Get(name);

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

            object value = parameter.Value;

            try
            {
                if (context.ModelType == typeof(String))
                {
                    return(value);
                }

                if (context.ModelType == typeof(Int32))
                {
                    return(value = Int32.Parse((string)value));
                }

                if (context.ModelType == typeof(Boolean))
                {
                    if ((string)value == "1" || ((string)value).ToUpper() == bool.TrueString.ToUpper())
                    {
                        return(true);
                    }
                    else if ((string)value == "0" || ((string)value).ToUpper() == bool.FalseString.ToUpper())
                    {
                        return(false);
                    }

                    return(null);
                }

                if (context.ModelType == typeof(Byte))
                {
                    return(value = Byte.Parse((string)value));
                }

                if (context.ModelType == typeof(SByte))
                {
                    return(value = SByte.Parse((string)value));
                }

                if (context.ModelType == typeof(Int16))
                {
                    return(value = Int16.Parse((string)value));
                }

                if (context.ModelType == typeof(UInt16))
                {
                    return(value = UInt16.Parse((string)value));
                }

                if (context.ModelType == typeof(Int32))
                {
                    return(value = Int32.Parse((string)value));
                }

                if (context.ModelType == typeof(UInt32))
                {
                    return(value = UInt32.Parse((string)value));
                }

                if (context.ModelType == typeof(Int64))
                {
                    return(value = Int64.Parse((string)value));
                }

                if (context.ModelType == typeof(UInt64))
                {
                    return(value = UInt64.Parse((string)value));
                }

                if (context.ModelType == typeof(Double))
                {
                    return(value = Double.Parse((string)value));
                }
            }
            catch (Exception ex)
            {
                throw new ModelBindingException(ex.Message, context.ModelName, value);
            }

            return(null);
        }
 /// <summary>
 /// Determines whether this instance can bind the specified model.
 /// </summary>
 /// <param name="context">Context infromation.</param>
 /// <returns>
 ///   <c>true</c> if this instance can handle the model; otherwise <c>false</c>.
 /// </returns>
 public bool CanBind(IModelBinderContext context)
 {
     return context.ModelType.IsEnum;
 }
示例#29
0
 //public Type Bind(Type typeBinder, IHttpRequest request, string name)
 //{
 //    var provider = new RequestValueProvider(request as IHttpMessage);
 //    if (!StringUtility.IsNullOrEmpty(name))
 //    {
 //        var context = new ModelBinderContext(typeBinder, name, "", provider);
 //        context.RootBinder = this;
 //        foreach (IModelBinder modelBinder in _binders)
 //        {
 //            if (modelBinder.CanBind(context))
 //            {
 //                return (typeBinder)modelBinder.Bind(context);
 //            }
 //        }
 //        return default(typeBinder);
 //    }
 //    //if (IEnumerable).IsInstanceOfTypetypeBinder)
 //    //    throw new InvalidOperationException("did not expect IEnumerable implementations without a name in the binder.");
 //    //var model = Activator.CreateInstance(typeof(typeBinder));
 //    var model = ServiceResolver.Current.Resolve(typeBinder);
 //    foreach (var property in model.GetType().GetProperties())
 //    {
 //        var context = new ModelBinderContext(property.PropertyType, property.Name, "", provider);
 //        context.RootBinder = this;
 //        foreach (var modelBinder in _binders)
 //        {
 //            if (modelBinder.CanBind(context))
 //            {
 //                var value = modelBinder.Bind(context);
 //                property.SetValue(model, value, null);
 //                break;
 //            }
 //        }
 //    }
 //    return (typeBinder)model;
 //}
 bool IModelBinder.CanBind(IModelBinderContext context)
 {
     return true;
 }
        /// <summary>
        /// Bind the model
        /// </summary>
        /// <param name="context">Context information</param>
        /// <returns>
        /// An object of the specified type (<seealso cref="IModelBinderContext.ModelType)" />
        /// </returns>
        public object Bind(IModelBinderContext context)
        {
            var name = StringUtility.IsNullOrEmpty(context.Prefix)
                           ? context.ModelName
                           : context.Prefix + "." + context.ModelName;
            var parameter = context.ValueProvider.Get(name);
            if (parameter == null)
                return null;

            object value = parameter.Value;

            try
            {
                if (context.ModelType == typeof(String))
                {
                    return value;
                }

                if (context.ModelType == typeof(Int32))
                {
                    return value =  Int32.Parse((string)value);
                }

                if (context.ModelType == typeof(Boolean))
                {

                    if ((string)value == "1" || ((string)value).ToUpper() == bool.TrueString.ToUpper())
                    {
                        return true;
                    }
                    else if ((string)value == "0" || ((string)value).ToUpper() == bool.FalseString.ToUpper())
                    {
                        return false;
                    }

                    return null;
                }

                if (context.ModelType == typeof(Byte))
                {
                    return value = Byte.Parse((string)value);
                }

                if (context.ModelType == typeof(SByte))
                {
                    return value = SByte.Parse((string)value);
                }

                if (context.ModelType == typeof(Int16))
                {
                    return value = Int16.Parse((string)value);
                }

                if (context.ModelType == typeof(UInt16))
                {
                    return value = UInt16.Parse((string)value);
                }

                if (context.ModelType == typeof(Int32))
                {
                    return value = Int32.Parse((string)value);
                }

                if (context.ModelType == typeof(UInt32))
                {
                    return value = UInt32.Parse((string)value);
                }

                if (context.ModelType == typeof(Int64))
                {
                    return value = Int64.Parse((string)value);
                }

                if (context.ModelType == typeof(UInt64))
                {
                    return value = UInt64.Parse((string)value);
                }

                if (context.ModelType == typeof(Double))
                {
                    return value = Double.Parse((string)value);
                }

            }
            catch (Exception ex)
            {
                throw new ModelBindingException(ex.Message, context.ModelName, value);
            }

            return null;
        }
 /// <summary>
 /// Determines whether this instance can bind the specified model.
 /// </summary>
 /// <param name="context">Context information.</param>
 /// <returns>
 ///   <c>true</c> if this instance can handle the model; otherwise <c>false</c>.
 /// </returns>
 public bool CanBind(IModelBinderContext context)
 {
     return context.ModelType == typeof(String)
         || context.ModelType == typeof(Boolean)
         || context.ModelType == typeof(Byte)
         || context.ModelType == typeof(SByte)
         || context.ModelType == typeof(Int16)
         || context.ModelType == typeof(UInt16)
         || context.ModelType == typeof(Int32)
         || context.ModelType == typeof(UInt32)
         || context.ModelType == typeof(Int64)
         || context.ModelType == typeof(UInt64)
         || context.ModelType == typeof(Double);
 }
示例#32
0
 /// <summary>
 /// Determines whether this instance can bind the specified model.
 /// </summary>
 /// <param name="context">Context infromation.</param>
 /// <returns>
 ///   <c>true</c> if this instance can handle the model; otherwise <c>false</c>.
 /// </returns>
 public bool CanBind(IModelBinderContext context)
 {
     return(context.ModelType.IsClass &&
            !context.ModelType.IsAbstract &&
            !context.ModelType.IsGenericType);
 }
 /// <summary>
 /// Determines whether this instance can bind the specified model.
 /// </summary>
 /// <param name="context">Context infromation.</param>
 /// <returns>
 ///   <c>true</c> if this instance can handle the model; otherwise <c>false</c>.
 /// </returns>
 public bool CanBind(IModelBinderContext context)
 {
     return(context.ModelType.IsPrimitive ||
            context.ModelType == typeof(string));
 }
示例#34
0
 /// <summary>
 /// Determines whether this instance can bind the specified model.
 /// </summary>
 /// <param name="context">Context information.</param>
 /// <returns>
 ///   <c>true</c> if this instance can handle the model; otherwise <c>false</c>.
 /// </returns>
 public bool CanBind(IModelBinderContext context)
 {
     return context.ModelType.IsClass
         && !context.ModelType.IsAbstract;
         //&& !context.ModelType.IsGenericType;
 }