Convert C # to VB.Net - using MVCContrib Blockrenderer to partially view a string

I need to display a partial view for a string, and I'm trying to convert a C # example to VB.Net since I am stuck with this for this project.

This causes me a headache of these two problems:

  • ObjectViewData - I Can't Understand What It Is
  • RenderPartial is a sub, but seems to be used as a function - I don't understand it

I refer to MVCContrib.UI, so I do not need to convert it. But these two functions, I need to convert:

(from 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(); } 

Here is my attempt to convert 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 

alt text http://i43.tinypic.com/2cg1ves.png

0
c # asp.net-mvc mvccontrib
source share
4 answers

This line:

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

NOT equivalent to the source line:

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

The C # example uses lambda, while the VB example just calls the method directly, which does not return a value. The compiler is not lying to you.

Try this instead:

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

I looked at Capture and expected Action, which is just a delegate, and it looks like the compiler cannot sign the delegate to wrap an anonymous function. Therefore, we will give him a helping hand and wrap the lambda.

+2
source share

You can do it manually or try using http://www.developerfusion.com/tools/convert/csharp-to-vb/

EDIT: also your code has

 View() 

in the end

 Public Function MakeStringForMe() 

it should be

 Return View() 

In response to paragraph 2, the code does not use the renderPartial routine using the RenderPartialToString function.

NTN

Oneshot

0
source share

RenderPartialToString must return a string, s

0
source share

My favorite converter can be found at this link.

The reason for my beloved is that it can be used "offline", that is, not on your web page. The converter is displayed as a web service, and there the sample code (in C #) refers to it.

I downloaded their sample code and adapted it for reading and writing from the file system. The conversion is made much easier ...

<edit> I know that the link doesn’t actually go to the converter - it goes to the "about" page, with links from it to the converter page and to download the sample code. In addition, I must remember that this is a triangular converter (VB, C # and Boo), bidirectional between any two languages, / Editing>

0
source share

All Articles