我需要将部分视图呈现为字符串,并且我正在尝试将 C# 示例转换为 VB.Net,因为我在这个项目中坚持使用它。
这让我对这两个问题感到头疼:
- ObjectViewData - 我不知道那是什么
- RenderPartial 是一个子,但似乎被用作一个函数 - 我不明白
我引用了 MVCContrib.UI,所以我不需要转换它。但是这两个函数,我确实需要转换:
(来自brightmix.com)
/// Static Method to render string - put somewhere of your choosing
public static string RenderPartialToString(string userControl, object viewData, ControllerContext controllerContext)
{
HtmlHelper h = new HtmlHelper(new ViewContext(controllerContext, new WebFormView("omg"), null, null), new ViewPage());
var blockRenderer = new BlockRenderer(controllerContext.HttpContext);
string s = blockRenderer.Capture(
() => RenderPartialExtensions.RenderPartial(h, userControl, viewData)
);
return s;
}
/// Your Controller method...
public ActionResult MakeStringForMe()
{
var objectViewData = new objectViewData { SomeString = "Dude", SomeNumber = 1 };
string s = RenderPartialToString("~/Views/Controls/UserControl.ascx", objectViewData, this.ControllerContext);
View();
}
这是我尝试将其转换为 VB.Net
'Static Method to render string - put somewhere of your choosing'
Public Shared Function RenderPartialToString(ByVal userControl As String, ByVal viewData As Object, ByVal controllerContext As ControllerContext) As String
Dim h As New HtmlHelper(New ViewContext(controllerContext, New WebFormView("omg"), Nothing, Nothing), New ViewPage())
Dim blockRenderer As New MvcContrib.UI.BlockRenderer(controllerContext.HttpContext)
Dim s As String = blockRenderer.Capture(RenderPartialExtensions.RenderPartial(h, UserControl, viewData))
End Function
Public Function MakeStringForMe() As ActionResult
Dim objectViewData As objectViewData = New objectViewData With {.SomeString = "Dude", .SomeNumber = 1}
Dim s As String = RenderPartialToString("~/Views/Controls/UserControl.ascx", objectViewData, Me.ControllerContext)
View()
End Function