将C#转换为VB.Net-使用MVCContrib Blockrenderer将部分视图呈现为字符串 [英] Convert C# to VB.Net - Using MVCContrib Blockrenderer to render a partial view to a string

查看:182
本文介绍了将C#转换为VB.Net-使用MVCContrib Blockrenderer将部分视图呈现为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将字符串的局部视图呈现出来,并且我试图将C#示例转换为VB.Net,因为我对此项目一无所知.

I need to render a partialview to a string, and I am trying to convert a C# example to VB.Net, as I am stuck with that for this project.

这使我从以下两个问题中感到头痛:

This is causing me a headache from these two problems:

  1. ObjectViewData-我不知道那是什么
  2. RenderPartial是一个子项,但似乎用作功能-我不明白

我引用了MVCContrib.UI,因此不需要进行转换.但是,这两个功能确实需要转换:

I reference the MVCContrib.UI, so I don't need to convert that. But these two functions, I do need to convert:

(来自 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

Here is my attempt at converting it to 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

推荐答案

此行:

Dim s As String = blockRenderer.Capture(RenderPartialExtensions.RenderPartial(h, UserControl, viewData))

不等同于您的原始行:

string s = blockRenderer.Capture(
         () => RenderPartialExtensions.RenderPartial(h, userControl, viewData)
     );

C#示例使用lambda,而VB示例只是直接调用该方法,而不会返回值.编译器没有骗你.

The C# example is using a lambda, while the VB example is just calling the method directly, which doesn't return a value. The compiler isn't lying to you.

尝试以下方法:

Dim s = blockRender.Capture(New Action(Of String)(Function() RenderPartialExtensions.RenderPartial(h, UserControl, viewData)))

我看了一下Capture,它期望一个Action只是一个委托,而且看起来编译器无法推断出委托的签名来包装匿名函数.因此,我们将为您提供帮助,并亲自包装lambda.

I took a look at Capture and it's expecting an Action which is just a delegate, and it looks like the compiler can't infer the delegate's signature to wrap the anonymous function. So we'll give it a helping hand and wrap the lambda ourselves.

这篇关于将C#转换为VB.Net-使用MVCContrib Blockrenderer将部分视图呈现为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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