這是可能的剃刀幫手解決這個問題。這有點優雅,但它爲我做了這份工作。
所以父視圖定義一個幫手:
@helper HtmlYouWantRenderedInAPartialView()
{
<blink>Attention!</blink>
}
然後,當你渲染部分,你通過這個輔助函數
@Html.Partial("somePartial", new ViewDataDictionary { { "OptionalSection1", (Func<HelperResult>)(HtmlYouWantRenderedInAPartialView) } })
然後局部視圖裏面調用這個幫手像所以
<div>@ViewData.RenderHelper("OptionalSection1")</div>
最後,你需要有這個擴展方法來簡化「調用」pa RT
public static HelperResult RenderHelper(this ViewDataDictionary<dynamic> viewDataDictionary, string helperName)
{
Func<HelperResult> helper = viewDataDictionary[helperName] as Func<HelperResult>;
if (helper != null)
{
return helper();
}
return null;
}
所以整點是要通過這個幫助的委託,然後在子視圖的話來說,內容呈現得到您想要的地方。
子視圖的最終結果是這樣的
<div><blink>Attention!</blink></div>
什麼解決辦法?我有一個佈局 - >頁面 - >部分。當部分在那裏時,我需要引用腳本/佈局並將其加載到頭部。任何非愚蠢的方式來做到這一點?無論如何,這個限制有什麼意義? – Shimmy 2012-11-26 01:33:27
@Shimmy你可以嘗試在ViewData中添加某種數據結構,以指定Layout頁面應該引用哪些東西。 – marcind 2012-11-26 23:50:47