Umbraco - Render.NET User Control (ascx) macro with Razor

I have a razor script in Umbraco that is quite complicated and I want it to display a macro in it at some point.

A macro called SuggestionBox is actually a user control (.ascx), and traditionally it refers to a template using

<umbraco:macro Alias="SuggestionBox" language="cshtml" runat="server"></umbraco:macro> 

But now I need to call it from a razor script, so I tried;

 @Html.Raw(umbraco.library.RenderMacroContent("SuggestionBox", Model.Id)) 

and:

 @RenderPage("SuggestionBox") 

No luck, as far as I am sure that I am wrong.

I read somewhere that this may not be feasible if the page is wrapped on the main page.

It works if I add it to the template, as I have traditionally:

  <umbraco:macro Alias="EventsRenderer" language="cshtml" runat="server"></umbraco:macro> <div class="talkingPointPanel"> <h3><umbraco:Item field="talkingPoinstSuggestionText" runat="server"></umbraco:Item></h3> <umbraco:macro Alias="SuggestionBox" language="cshtml" runat="server"></umbraco:macro> </div> 

Where EventsRenderer displays a page that should ideally contain a hint.

using

 @Html.Raw(umbraco.library.RenderMacroContent("<?UMBRACO_MACRO macroAlias=\"SuggestionBox\" />", Model.Id)) 

Gives me this error:

 <!-- Error generating macroContent: 'System.Web.HttpException (0x80004005): HtmlForm cannot render without a reference to the Page instance. Make sure your form has been added to the control tree. at System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTextWriter output) at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) at umbraco.presentation.templateControls.Macro.Render(HtmlTextWriter writer) at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) at umbraco.library.RenderMacroContent(String Text, Int32 PageId)' --> 

Any ideas?

+4
source share
3 answers
 <umbraco:Macro runat="server" language="cshtml">@{ HtmlTextWriter writer = new HtmlTextWriter(this.Output); var navigation = new umbraco.presentation.templateControls.Macro(); navigation.Alias = "Navigation"; navigation.MacroAttributes.Add("ulclass", "art-vmenu"); navigation.MacroAttributes.Add("level", 2); navigation.RenderControl(writer); }</umbraco:Macro> 

Try something like this. This works for me ... I created a navigation macro. Be aware, although your variables must be specified in toLower, if caps are used, the parameters will not pass.

+9
source

In Umbraco 4.10+ To invoke a macro inside a Razor script, use:

@ Umbraco.RenderMacro ("macroNameHere", new {propertyName1 = CurrentPage.pageProperty}))

+2
source

Try something like this:

 @Html.Raw(umbraco.library.RenderMacroContent("<?UMBRACO_MACRO macroAlias=\"SuggestionBox\" />", Model.Id)) 
0
source

Source: https://habr.com/ru/post/1413572/


All Articles