Beispiel #1
0
        /// <summary>
        /// Renders a given Entity Model.
        /// </summary>
        /// <param name="htmlHelper">The HtmlHelper instance on which the extension method operates.</param>
        /// <param name="entity">The Entity to render.</param>
        /// <param name="containerSize">The size (in grid column units) of the containing element.</param>
        /// <returns>The rendered HTML or an empty string if <paramref name="entity"/> is <c>null</c>.</returns>
        public static MvcHtmlString DxaEntity(this HtmlHelper htmlHelper, EntityModel entity, int containerSize = 0)
        {
            if (entity == null)
            {
                return(MvcHtmlString.Empty);
            }

            if (containerSize == 0)
            {
                containerSize = SiteConfiguration.MediaHelper.GridSize;
            }

            MvcData mvcData = entity.MvcData;

            using (new Tracer(htmlHelper, entity, containerSize, mvcData))
            {
                if (mvcData == null)
                {
                    throw new DxaException($"Unable to render Entity Model [{entity}], because it has no MVC data.");
                }

                string actionName         = mvcData.ActionName ?? SiteConfiguration.GetEntityAction();
                string controllerName     = mvcData.ControllerName ?? SiteConfiguration.GetEntityController();
                string controllerAreaName = mvcData.ControllerAreaName ?? SiteConfiguration.GetDefaultModuleName();

                RouteValueDictionary parameters = new RouteValueDictionary();
                int parentContainerSize         = (int)htmlHelper.ViewData[DxaViewDataItems.ContainerSize];
                if (parentContainerSize == 0)
                {
                    parentContainerSize = SiteConfiguration.MediaHelper.GridSize;
                }
                parameters["containerSize"] = (containerSize * parentContainerSize) / SiteConfiguration.MediaHelper.GridSize;
                parameters["entity"]        = entity;
                parameters["area"]          = controllerAreaName;
                if (mvcData.RouteValues != null)
                {
                    foreach (string key in mvcData.RouteValues.Keys)
                    {
                        parameters[key] = mvcData.RouteValues[key];
                    }
                }

                MvcHtmlString result = htmlHelper.Action(actionName, controllerName, parameters);
                // If the Entity is being rendered inside a Region (typical), we don't have to transform the XPM markup attributes here; it will be done in DxaRegion.
                if (!(htmlHelper.ViewData.Model is RegionModel) && WebRequestContext.IsPreview)
                {
                    result = new MvcHtmlString(Markup.TransformXpmMarkupAttributes(result.ToString()));
                }
                return(Markup.DecorateMarkup(result, entity));
            }
        }
 private static bool IsCustomAction(MvcData mvcData)
 {
     return(mvcData.ActionName != SiteConfiguration.GetEntityAction() ||
            mvcData.ControllerName != SiteConfiguration.GetEntityController() ||
            mvcData.ControllerAreaName != SiteConfiguration.GetDefaultModuleName());
 }