/// <summary> /// Renders a given Region Model /// </summary> /// <param name="htmlHelper">The HtmlHelper instance on which the extension method operates.</param> /// <param name="region">The Region Model to render. This object determines the View that will be used.</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="region"/> is <c>null</c>.</returns> public static MvcHtmlString DxaRegion(this HtmlHelper htmlHelper, RegionModel region, int containerSize = 0) { if (region == null) { return(MvcHtmlString.Empty); } if (containerSize == 0) { containerSize = SiteConfiguration.MediaHelper.GridSize; } using (new Tracer(htmlHelper, region, containerSize)) { MvcData mvcData = region.MvcData; string actionName = mvcData.ActionName ?? SiteConfiguration.GetRegionAction(); string controllerName = mvcData.ControllerName ?? SiteConfiguration.GetRegionController(); string controllerAreaName = mvcData.ControllerAreaName ?? SiteConfiguration.GetDefaultModuleName(); MvcHtmlString result = htmlHelper.Action(actionName, controllerName, new { Region = region, containerSize = containerSize, area = controllerAreaName }); if (WebRequestContext.IsPreview) { result = new MvcHtmlString(Markup.TransformXpmMarkupAttributes(result.ToString())); } return(Markup.DecorateMarkup(result, region)); } }
/// <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)); } }