Beispiel #1
0
        public static MvcHtmlString Action <T>(this HtmlHelper htmlHelper, T model)
        {
            var factory = ActionFactory.Actions[typeof(T)];
            var url     = new UrlHelper(htmlHelper.ViewContext.RequestContext);

            return(ChildActionExtensions.Action(htmlHelper, factory.Action, factory.Controller, UrlExtensions.GenerateDict(model, url)));
        }
Beispiel #2
0
        /// <summary>
        /// Renders the Partial/Action
        /// </summary>
        /// <param name="writer"></param>
        public override void Render(HtmlTextWriter writer)
        {
            if (writer != null)
            {
                string writerString = string.Empty;

                if (!string.IsNullOrEmpty(this.ActionName))
                {
                    //calls the Action method
                    if (null == RouteValueDictionary)
                    {
                        writerString = ChildActionExtensions.Action(HtmlHelper, ActionName, ControllerName, RouteValues).ToString();
                    }
                    else
                    {
                        writerString = ChildActionExtensions.Action(HtmlHelper, ActionName, ControllerName, RouteValueDictionary).ToString();
                    }
                }
                else if (!string.IsNullOrEmpty(this.PartialViewName))
                {
                    //calls the partial method
                    writerString = PartialExtensions.Partial(HtmlHelper, PartialViewName, ViewModel, ViewData).ToString();
                }
                else
                {
                    base.Render(writer);
                }
                if (!string.IsNullOrEmpty(writerString))
                {
                    writer.Write(writerString);
                }
            }
        }
        public void ServerExecuteCalledWithWrappedChildActionMvcHandler()
        {
            // Arrange
            IHttpHandler callbackHandler      = null;
            TextWriter   callbackTextWriter   = null;
            bool         callbackPreserveForm = false;

            httpContext.Setup(hc => hc.Server.Execute(It.IsAny <IHttpHandler>(), It.IsAny <TextWriter>(), It.IsAny <bool>()))
            .Callback <IHttpHandler, TextWriter, bool>(
                (handler, textWriter, preserveForm) =>
            {
                callbackHandler      = handler;
                callbackTextWriter   = textWriter;
                callbackPreserveForm = preserveForm;
            });
            TextWriter stringWriter = new StringWriter();

            // Act
            ChildActionExtensions.ActionHelper(htmlHelper.Object, "actionName", null /* controllerName */, null /* routeValues */, stringWriter);

            // Assert
            Assert.NotNull(callbackHandler);
            HttpHandlerUtil.ServerExecuteHttpHandlerWrapper wrapper = callbackHandler as HttpHandlerUtil.ServerExecuteHttpHandlerWrapper;
            Assert.NotNull(wrapper);
            Assert.NotNull(wrapper.InnerHandler);
            ChildActionExtensions.ChildActionMvcHandler childHandler = wrapper.InnerHandler as ChildActionExtensions.ChildActionMvcHandler;
            Assert.NotNull(childHandler);
            Assert.Same(stringWriter, callbackTextWriter);
            Assert.True(callbackPreserveForm);
        }
        // ChildActionExtensions

        public static MvcHtmlString Action(this HtmlHelper htmlHelper, string actionName, string controllerName = null, object routeValues = null)
        {
            return(ChildActionExtensions.Action(
                       htmlHelper,
                       actionName,
                       controllerName,
                       routeValues as IDictionary <string, object> ?? new RouteValueDictionary(routeValues)
                       ));
        }
 public static void RenderAction(this HtmlHelper htmlHelper, string actionName, string controllerName = null, object routeValues = null)
 {
     ChildActionExtensions.RenderAction(
         htmlHelper,
         actionName,
         controllerName,
         routeValues as IDictionary <string, object> ?? new RouteValueDictionary(routeValues)
         );
 }
        public void NoMatchingRouteThrows()
        {
            // Arrange
            routes.Clear();

            // Act & Assert
            Assert.Throws <InvalidOperationException>(
                () => ChildActionExtensions.ActionHelper(htmlHelper.Object, "actionName", null /* controllerName */, null /* routeValues */, null /* textWriter */),
                MvcResources.Common_NoRouteMatched
                );
        }
        public void RouteDataTokensIncludesParentActionViewContext()
        {
            // Arrange
            MvcHandler mvcHandler = null;

            httpContext.Setup(hc => hc.Server.Execute(It.IsAny <IHttpHandler>(), It.IsAny <TextWriter>(), It.IsAny <bool>()))
            .Callback <IHttpHandler, TextWriter, bool>((handler, _, __) => mvcHandler = (MvcHandler)((HttpHandlerUtil.ServerExecuteHttpHandlerWrapper)handler).InnerHandler);

            // Act
            ChildActionExtensions.ActionHelper(htmlHelper.Object, "actionName", null /* controllerName */, null /* routeValues */, null /* textWriter */);

            // Assert
            Assert.Same(viewContext, mvcHandler.RequestContext.RouteData.DataTokens[ControllerContext.ParentActionViewContextToken]);
        }
Beispiel #8
0
        public void PassedRouteValuesOverrideParentRequestRouteValues()
        {
            // Arrange
            _originalRouteData.Values["name1"] = "value1";
            _originalRouteData.Values["name2"] = "value2";
            MvcHandler mvcHandler = null;

            _httpContext
            .Setup(
                hc =>
                hc.Server.Execute(
                    It.IsAny <IHttpHandler>(),
                    It.IsAny <TextWriter>(),
                    It.IsAny <bool>()
                    )
                )
            .Callback <IHttpHandler, TextWriter, bool>(
                (handler, _, __) =>
                mvcHandler = (MvcHandler)(
                    (HttpHandlerUtil.ServerExecuteHttpHandlerWrapper)handler
                    ).InnerHandler
                );

            // Act
            ChildActionExtensions.ActionHelper(
                _htmlHelper.Object,
                "actionName",
                null /* controllerName */
                ,
                new RouteValueDictionary {
                { "name2", "newValue2" }
            },
                null /* textWriter */
                );

            // Assert
            RouteData routeData = mvcHandler.RequestContext.RouteData;

            Assert.Equal("value1", routeData.Values["name1"]);
            Assert.Equal("newValue2", routeData.Values["name2"]);

            Assert.Equal(
                "newValue2",
                (
                    routeData.Values[ChildActionValueProvider.ChildActionValuesKey]
                    as DictionaryValueProvider <object>
                ).GetValue("name2").RawValue
                );
        }
Beispiel #9
0
        public void GuardClauses()
        {
            // Act & Assert
            Assert.ThrowsArgumentNull(
                () =>
                ChildActionExtensions.ActionHelper(
                    null     /* htmlHelper */
                    ,
                    "abc",
                    null     /* controllerName */
                    ,
                    null     /* routeValues */
                    ,
                    null     /* textWriter */
                    ),
                "htmlHelper"
                );

            Assert.ThrowsArgumentNullOrEmpty(
                () =>
                ChildActionExtensions.ActionHelper(
                    _htmlHelper.Object,
                    null     /* actionName */
                    ,
                    null     /* controllerName */
                    ,
                    null     /* routeValues */
                    ,
                    null     /* textWriter */
                    ),
                "actionName"
                );

            Assert.ThrowsArgumentNullOrEmpty(
                () =>
                ChildActionExtensions.ActionHelper(
                    _htmlHelper.Object,
                    String.Empty /* actionName */
                    ,
                    null         /* controllerName */
                    ,
                    null         /* routeValues */
                    ,
                    null         /* textWriter */
                    ),
                "actionName"
                );
        }
        public void NoChildActionValuesDictionaryCreatedIfNoRouteValuesPassed()
        {
            // Arrange
            MvcHandler mvcHandler = null;

            httpContext.Setup(hc => hc.Server.Execute(It.IsAny <IHttpHandler>(), It.IsAny <TextWriter>(), It.IsAny <bool>()))
            .Callback <IHttpHandler, TextWriter, bool>((handler, _, __) => mvcHandler = (MvcHandler)((HttpHandlerUtil.ServerExecuteHttpHandlerWrapper)handler).InnerHandler);

            // Act
            ChildActionExtensions.ActionHelper(htmlHelper.Object, "actionName", null /* controllerName */, null, null /* textWriter */);

            // Assert
            RouteData routeData = mvcHandler.RequestContext.RouteData;

            Assert.Null(routeData.Values[ChildActionValueProvider.ChildActionValuesKey]);
        }
        public void RouteValuesIncludeNewActionName()
        {
            // Arrange
            MvcHandler mvcHandler = null;

            httpContext.Setup(hc => hc.Server.Execute(It.IsAny <IHttpHandler>(), It.IsAny <TextWriter>(), It.IsAny <bool>()))
            .Callback <IHttpHandler, TextWriter, bool>((handler, _, __) => mvcHandler = (MvcHandler)((HttpHandlerUtil.ServerExecuteHttpHandlerWrapper)handler).InnerHandler);

            // Act
            ChildActionExtensions.ActionHelper(htmlHelper.Object, "actionName", null /* controllerName */, null /* routeValues */, null /* textWriter */);

            // Assert
            RouteData routeData = mvcHandler.RequestContext.RouteData;

            Assert.Equal("actionName", routeData.Values["action"]);
        }
Beispiel #12
0
        public void RouteValuesDoesNotIncludeExplicitlyPassedAreaName()
        {
            // Arrange
            Route route = _routes.MapRoute("my-area", "my-area");

            route.DataTokens["area"] = "myArea";
            MvcHandler mvcHandler = null;

            _httpContext
            .Setup(
                hc =>
                hc.Server.Execute(
                    It.IsAny <IHttpHandler>(),
                    It.IsAny <TextWriter>(),
                    It.IsAny <bool>()
                    )
                )
            .Callback <IHttpHandler, TextWriter, bool>(
                (handler, _, __) =>
                mvcHandler = (MvcHandler)(
                    (HttpHandlerUtil.ServerExecuteHttpHandlerWrapper)handler
                    ).InnerHandler
                );

            // Act
            ChildActionExtensions.ActionHelper(
                _htmlHelper.Object,
                "actionName",
                null /* controllerName */
                ,
                new RouteValueDictionary {
                { "area", "myArea" }
            },
                null /* textWriter */
                );

            // Assert
            RouteData routeData = mvcHandler.RequestContext.RouteData;

            Assert.False(routeData.Values.ContainsKey("area"));
            Assert.Null(
                (
                    routeData.Values[ChildActionValueProvider.ChildActionValuesKey]
                    as DictionaryValueProvider <object>
                ).GetValue("area")
                );
        }
Beispiel #13
0
        private MvcHtmlString RenderComponentPresentation(IComponentPresentation cp, HtmlHelper htmlHelper)
        {
            string controller = WebConfigurationManager.AppSettings["Controller"];
            string action     = WebConfigurationManager.AppSettings["Action"];


            if (cp.ComponentTemplate.MetadataFields.ContainsKey("controller"))
            {
                controller = cp.ComponentTemplate.MetadataFields["controller"].Value;
            }
            if (cp.ComponentTemplate.MetadataFields.ContainsKey("action"))
            {
                action = cp.ComponentTemplate.MetadataFields["action"].Value;
            }

            return(ChildActionExtensions.Action(htmlHelper, action, controller, new { componentPresentation = cp }));
        }
        public void RouteValuesIncludeNewControllerNameWhenControllNameIsNotEmpty()
        {
            // Arrange
            originalRouteData.Values["controller"] = "oldController";
            MvcHandler mvcHandler = null;

            httpContext.Expect(hc => hc.Server.Execute(It.IsAny <IHttpHandler>(), It.IsAny <TextWriter>(), It.IsAny <bool>()))
            .Callback <IHttpHandler, TextWriter, bool>((handler, _, __) => mvcHandler = (MvcHandler)((HttpHandlerUtil.ServerExecuteHttpHandlerWrapper)handler).InnerHandler);

            // Act
            ChildActionExtensions.ActionHelper(htmlHelper.Object, "actionName", "newController", null /* routeValues */, null /* textWriter */);

            // Assert
            RouteData routeData = mvcHandler.RequestContext.RouteData;

            Assert.AreEqual("newController", routeData.Values["controller"]);
        }
        public void ActionHelper_ChildAction_WithActionDirectRoute()
        {
            // Arrange
            AttributeRoutingMapper.MapAttributeRoutes(_routes, new Type[] { typeof(DirectRouteActionController) });

            MvcHandler mvcHandler = null;

            _httpContext.Setup(hc => hc.Server.Execute(It.IsAny <IHttpHandler>(), It.IsAny <TextWriter>(), It.IsAny <bool>()))
            .Callback <IHttpHandler, TextWriter, bool>((handler, _, __) => mvcHandler = (MvcHandler)((HttpHandlerUtil.ServerExecuteHttpHandlerWrapper)handler).InnerHandler);

            // Act
            ChildActionExtensions.ActionHelper(_htmlHelper.Object, "Action", null /* controllerName */, null /* routeValues */, null /* textWriter */);

            // Assert
            RouteData routeData = mvcHandler.RequestContext.RouteData;

            Assert.Equal("Action", routeData.Values["action"]);
        }
        public void RouteValuesDoesIncludesExplicitlyPassedAreaNameIfAreasNotInUse()
        {
            // Arrange
            Route      route      = routes.MapRoute("my-area", "my-area");
            MvcHandler mvcHandler = null;

            httpContext.Expect(hc => hc.Server.Execute(It.IsAny <IHttpHandler>(), It.IsAny <TextWriter>(), It.IsAny <bool>()))
            .Callback <IHttpHandler, TextWriter, bool>((handler, _, __) => mvcHandler = (MvcHandler)((HttpHandlerUtil.ServerExecuteHttpHandlerWrapper)handler).InnerHandler);

            // Act
            ChildActionExtensions.ActionHelper(htmlHelper.Object, "actionName", null /* controllerName */, new RouteValueDictionary {
                { "area", "myArea" }
            }, null /* textWriter */);

            // Assert
            RouteData routeData = mvcHandler.RequestContext.RouteData;

            Assert.IsTrue(routeData.Values.ContainsKey("area"));
        }
Beispiel #17
0
        public void HandleEvent(TabStripCreated eventMessage)
        {
            string tabStripName = eventMessage.TabStripName;
            int    entityId     = ((EntityModelBase)eventMessage.Model).Id;
            string entityName   = eventMessage.TabStripName.Substring(0, eventMessage.TabStripName.IndexOf("-"));

            if (tabStripName == "category-edit" || tabStripName == "article-edit")
            {
                eventMessage.ItemFactory.Add().Text("Banner").Name("tab-custombanner").Icon("fa fa-picture-o fa-lg fa-fw").LinkHtmlAttributes(new
                {
                    data_tab_name = "CustomBanner"
                }).Route("CAF.WebSite.CustomBanner", new
                {
                    action     = "PictureEditTab",
                    entityId   = entityId,
                    entityName = entityName
                }).Ajax(true);
            }
            if (tabStripName == "topic-edit")
            {
                eventMessage.ItemFactory.Add().Text("Banner").Name("tab-custombanner").Icon("fa fa-picture-o fa-lg fa-fw").LinkHtmlAttributes(new
                {
                    data_tab_name = "CustomBanner"
                }).Content(ChildActionExtensions.Action(eventMessage.Html, "PictureEditTab", "CustomBanner", new
                {
                    action     = "PictureEditTab",
                    entityId   = entityId,
                    entityName = entityName,
                    area       = "CAF.WebSite.CustomBanner"
                }).ToHtmlString()).Route("CAF.WebSite.CustomBanner", new
                {
                    action     = "PictureEditTab",
                    entityId   = entityId,
                    entityName = entityName
                });
            }
        }
Beispiel #18
0
 public static MvcHtmlString Action(this HtmlHelper htmlHelper, string action)
 {
     return(ChildActionExtensions.Action(htmlHelper, action));
 }
Beispiel #19
0
 public static MvcHtmlString Action(this HtmlHelper htmlHelper, string action, string controller, object routevalues)
 {
     return(ChildActionExtensions.Action(htmlHelper, action, controller, routevalues));
 }