我在Umbraco有一个非常复杂的剃刀脚本,我想在它的某个方面渲染一个宏。
名为SuggestionBox的宏实际上是一个用户控件(.ascx),传统上这是在模板上使用
引用的<umbraco:macro Alias="SuggestionBox" language="cshtml" runat="server"></umbraco:macro>
但是现在我需要从剃刀脚本中调用它,所以我尝试了;
@Html.Raw(umbraco.library.RenderMacroContent("SuggestionBox", Model.Id))
以及:
@RenderPage("SuggestionBox")
到目前为止,我确定我错误地使用了这些。
如果页面包含在母版页中,我在某处读到它可能是不可行的。
如果我像传统上那样将它添加到模板中,它会起作用:
<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>
其中EventsRenderer呈现理想情况下应包含SuggestionBox的页面。
使用
@Html.Raw(umbraco.library.RenderMacroContent("<?UMBRACO_MACRO macroAlias=\"SuggestionBox\" />", Model.Id))
给我这个错误:
<!-- 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)' -->
有什么想法吗?
答案 0 :(得分:9)
<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>
尝试这样的事情。它对我有用......我制作了一个导航宏。要注意,虽然您的变量应该在toLower中给出,但如果使用了上限,则参数将无法通过。
答案 1 :(得分:2)
在Umbraco 4.10+中要在Razor脚本中调用宏,请使用:
@ Umbraco.RenderMacro(“macroNameHere”,new {propertyName1 = CurrentPage.pageProperty}))
答案 2 :(得分:0)
尝试这样的事情:
@Html.Raw(umbraco.library.RenderMacroContent("<?UMBRACO_MACRO macroAlias=\"SuggestionBox\" />", Model.Id))