Beispiel #1
0
        /// <summary>
        /// Binds all arguments from the Action method to objects and returns the list.
        /// </summary>
        /// <param name="requestContext"></param>
        /// <param name="controller"></param>
        /// <param name="func"></param>
        /// <returns></returns>
        private static List <object> BindArgumentsFromControllerAction(RequestContext requestContext, IController controller, MethodInfo func)
        {
            // Iterate through all arguments of the action method to bind data to them from Request.Form
            List <object> parameters = new List <object>();

            foreach (var p in func.GetParameters())
            {
                object arg;
                if (p.ParameterType.Name == "String")
                {
                    arg = String.Empty;
                }
                else
                {
                    arg = Activator.CreateInstance(p.ParameterType);
                }

                // If primitive type or String then just bind the data otherwise bind request data to the class.
                if (arg.GetType().IsPrimitive || arg.GetType().Name == "String")
                {
                    if (requestContext.HttpContext.Request.Form[p.Name] != null)
                    {
                        arg = Convert.ChangeType(requestContext.HttpContext.Request.Form[p.Name], p.ParameterType);
                    }
                }
                else
                {
                    var modelBinder = new DefaultModelBinder(arg, requestContext.HttpContext.Request.Form);
                    modelBinder.Bind();
                    controller.ModelState = modelBinder.ModelState;
                }
                parameters.Add(arg);
            }
            return(parameters);
        }
 public void Model_Validation_For_Required_Field_Missing_Fails()
 {
     var model = new Fakes.FakeHomeViewModel();
     var values = new NameValueCollection();
     var binder = new DefaultModelBinder(model, values);
     binder.Bind();
     Assert.IsTrue(binder.ModelState.HasErrors);
 }
 public void Model_Validation_For_Regular_Expression_Check_Passes()
 {
     var model = new Fakes.FakeHomeViewModel() { HelloWorld = "Hello Mvc!", CreatedDate = "11/1/2010" };
     var values = new NameValueCollection();
     var binder = new DefaultModelBinder(model, values);
     binder.Bind();
     Assert.IsFalse(binder.ModelState.HasErrors);
 }
 public void Model_Validation_For_Range_Check_Fails()
 {
     var model = new Fakes.FakeHomeViewModel() { HelloWorld = "Hello Mvc!", CreatedDate = "11/1/2010", SomeInt = 10 };
     var values = new NameValueCollection();
     var binder = new DefaultModelBinder(model, values);
     binder.Bind();
     Assert.IsTrue(binder.ModelState.HasErrors);
 }
        public void Bind_Model_From_Request_Form_Values_Success()
        {
            var model = new Fakes.FakeHomeViewModel();
            var values = new NameValueCollection();
            values.Add("HelloWorld", "Hello Mvc!");

            var binder = new DefaultModelBinder(model, values);

            binder.Bind();

            Assert.IsFalse(binder.ModelState.HasErrors);
            Assert.AreEqual("Hello Mvc!", model.HelloWorld);
        }
        /// <summary>
        /// Binds all arguments from the Action method to objects and returns the list.
        /// </summary>
        /// <param name="requestContext"></param>
        /// <param name="controller"></param>
        /// <param name="func"></param>
        /// <returns></returns>
        private static List<object> BindArgumentsFromControllerAction(RequestContext requestContext, IController controller, MethodInfo func)
        {
            // Iterate through all arguments of the action method to bind data to them from Request.Form
            List<object> parameters = new List<object>();
            foreach (var p in func.GetParameters()) {
                object arg;
                if (p.ParameterType.Name == "String")
                    arg = String.Empty;
                else
                    arg = Activator.CreateInstance(p.ParameterType);

                // If primitive type or String then just bind the data otherwise bind request data to the class.
                if (arg.GetType().IsPrimitive || arg.GetType().Name == "String") {
                    if (requestContext.HttpContext.Request.Form[p.Name] != null)
                        arg = Convert.ChangeType(requestContext.HttpContext.Request.Form[p.Name], p.ParameterType);
                }
                else {
                    var modelBinder = new DefaultModelBinder(arg, requestContext.HttpContext.Request.Form);
                    modelBinder.Bind();
                    controller.ModelState = modelBinder.ModelState;
                }
                parameters.Add(arg);

            }
            return parameters;
        }