@TheKaneda,謝謝你的見解。我把你的想法和擴展它,這樣你就提供了一個PartialView名稱,它知道如何解析它。
<Extension()> _
Public Function UseTemplate(ByVal html As HtmlHelper, ByVal PartialView As String) As IDisposable
Return New TemplateWrapper(html, PartialView)
End Function
Public Class TemplateWrapper
Implements IDisposable
Private _HtmlHelper As HtmlHelper
'Index 0 is header
'Index 1 is footer
Private _TemplateWrapper As String()
Public Sub New(ByVal Html As HtmlHelper, ByVal PartialView As String)
_TemplateWrapper = Html.Partial(PartialView).ToHtmlString.Split("@@RenderBody()")
_HtmlHelper = Html
_HtmlHelper.ViewContext.Writer.Write(_TemplateWrapper(0))
End Sub
Public Sub Dispose() Implements IDisposable.Dispose
_HtmlHelper.ViewContext.Writer.Write(_TemplateWrapper(1).Substring(12))
End Sub
End Class
使用與@ TheKaneda示例相同的用法。在你的局部視圖中,不要調用@RenderBody(),只需將@@ RenderBody()作爲內容中間部分的標誌。對不起VB翻譯。
使用我的用法的一個例子。
Using Html.UseTemplate("ContentWrapper")
@Html.EditorFor(Function(m) m.Var1, "TemplateHint")
@Html.EditorFor(Function(m) m.Var2, "TemplateHint")
@Html.EditorFor(Function(m) m.Var3)
End Using
我的部分看起來像這樣...
<div class="content">
@@RenderBody()
</div>
這正是我一直在尋找的。 – Josh 2012-02-01 14:02:10
有一個簡單的例子在http://stackoverflow.com/questions/7196276/creating-mvc3-razor-helper-like-helper-beginform – 2013-03-07 11:14:27
我很難看到是什麼使它更簡單,考慮到解決方案正是相同......在這個例子中,唯一的附加代碼就是爲了滿足問題的要求 - 還有一個基本的檢查,以避免Dispose的副作用被兩次調用。 – JimmiTh 2013-03-07 12:07:50