protected static dynamic dynamicizeCommonObject(dynamic obj)
 {
     dynamic model = obj;
     if (model is StringObject)
     {
         model = model.Value.Value;
     }
     if (model is BoxedValue)
     {
         model = model.ClrBoxed;
     }
     if (model is CommonObject)
     {
         model = new DynamicCommonObject(model);
     }
     return model;
 }
        protected ActionResult InvokeAction(ControllerContext controllerContext, string controllerName, string actionName)
        {
            var scriptPath = controllerContext.HttpContext.Server.MapPath(string.Format("~/Controllers/{0}.js", controllerName));

            var context = IronJSControllerActionInvoker.initializeIronJSContext(controllerContext);
            CommonObject jscontroller = null;

            try
            {
                jscontroller = context.ExecuteFile(scriptPath) as CommonObject;
                RegisterControllerMembers(context, controllerContext, jscontroller);
            }
            catch (FileNotFoundException ex)
            {
                throw new Exception(string.Format("IronJSMVC: Controller '{0}' not found.", controllerName), ex);
            }

            if (!jscontroller.Members.Keys.Contains(actionName))
            {
                throw new Exception(string.Format("IronJSMVC: Action '{1}' does not exist within Controller '{0}'", controllerName, actionName));
            }

            BoxedValue rawModel;
            var actionFunc = jscontroller.Members[actionName] as FunctionObject;
            var id = controllerContext.RouteData.Values["id"] as string;
            if (id == null)
            {
                rawModel = actionFunc.Call(jscontroller);
            }
            else
            {
                rawModel = actionFunc.Call<string>(jscontroller, id);
            }

            dynamic model = rawModel;
            if (model is BoxedValue)
            {
                model = model.ClrBoxed;
            }
            if (model is CommonObject)
            {
                model = new DynamicCommonObject(model);
            }

            ViewResult viewResult = null;
            if (model is ViewResult)
            {
                // the action method returned an ActionResult, so just use that
                viewResult = (ViewResult)model;
            }
            else
            {
                if (model != null)
                {
                    controllerContext.Controller.ViewData.Model = model;
                }
                viewResult = this.JSView(controllerContext, actionName, null, model);
            }

            if (string.IsNullOrWhiteSpace(viewResult.MasterName))
            {
                // Get the Controller's Default Master Layout
                if (jscontroller.Members.ContainsKey("_Layout"))
                {
                    viewResult.MasterName = jscontroller.Members["_Layout"] as String;
                }
            }

            return viewResult;
        }