Convert C# to VB.Net - Using MVCContrib Blockrenderer to render a partial view to a string
Asked Answered
R

4

0

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 - I can't figure out what that is
  2. RenderPartial is a sub, but seems to be used as a function - I don' get it

I reference the MVCContrib.UI, so I don't need to convert that. But these two functions, I do 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 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

alt text

Roxana answered 11/6, 2009 at 13:5 Comment(0)
B
2

This line:

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

is NOT equivalent to your original line:

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

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.

Try this instead:

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

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.

Brodench answered 11/6, 2009 at 13:36 Comment(2)
Thanks for your answer. I knew my attempt was not correct, but thought it would make it easier to get good answers. Dim s = blockRender.Capture(Function() RenderPartialExtensions.RenderPartial(h, UserControl, viewData)) This still returns an error "Expression does not produce a value"Roxana
Looks like it's not working anymore with VS 2010 / C# 4.0 / MVC 2 (dunno which one causes the problem as I migrated all at the same time) => Do you know any way to have it back ? I'm massively using this mechanism to render all controls of my application...Wean
P
0

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

EDIT: also your code has

View() 

at the end of

Public Function MakeStringForMe() 

this should be

Return View()

In response to point 2 the code isn't using the renderPartial sub it is using the RenderPartialToString function.

HTH

OneSHOT

Parceling answered 11/6, 2009 at 13:7 Comment(1)
Problem 2 refers to the first function, RenderPartialToString, where something I think is a lambda expression is being used.Roxana
S
0

RenderPartialToString needs to return a string, s

Squish answered 11/6, 2009 at 13:35 Comment(0)
P
0

My favorite converter can be found at this link

The reason it's my favorite is that it can be used "offline"--that is, not at their web page. The converter is exposed as a web service, and there's sample code (in C#) to reference it.

I downloaded their sample code & adapted it to read & write from the file system. Made a conversion a whole lot simpler....

< edit> I know that link doesn't actually go to the converter--it goes to an "about" page, with links from there to the converter page & to the sample code download. Also, I should prob'ly mention that it's a three-cornered converter (VB, C# and Boo), bidirectional between any two languages < /edit>

Puccoon answered 11/6, 2009 at 15:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.