Umbraco-使用Razor渲染.Net用户控件(ascx)宏 [英] Umbraco - Render .Net User Control (ascx) macro with Razor

查看:89
本文介绍了Umbraco-使用Razor渲染.Net用户控件(ascx)宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Umbraco中有一个剃刀脚本,它非常复杂,我希望在某个时候在其中渲染一个宏.

I have a razor script in Umbraco that is quite complex and I want at some point of it to render a macro in it.

被称为"SuggestionBox"的宏实际上是一个用户控件(.ascx),传统上,它是使用

The macro which is called SuggestionBox is actually a user control (.ascx) and traditionally this is referenced on the template using

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

但是现在我需要从剃刀脚本中调用它,所以我尝试了;

But now I need to call it from the razor script instead so I tried;

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

以及:

@RenderPage("SuggestionBox")

就我确定我错误地使用了这些而言,没有运气.

No luck so far as I'm sure I'm using these wrongly.

我读过某个地方,如果该页面包装在母版页中可能是不可行的.

I read somewhere it might be infeasible if the page is wrapped in a masterpage.

如果我像往常一样将其添加到模板中,它将起作用:

It works if I add it to the Template like I traditionally would:

 <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.

Where EventsRenderer renders the page that should ideally contain the 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)' -->

有什么想法吗?

推荐答案

<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中指定变量,但是如果使用大写字母,则参数将不会通过.

Try something like this. It works for me ... I have made a Navigation macro. Be Aware though your variables should be given in toLower, if caps are used, the parameters will not come through.

这篇关于Umbraco-使用Razor渲染.Net用户控件(ascx)宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆