Note that there are some explanatory texts on larger screens.

plurals
  1. POConvert C# to VB.Net - Using MVCContrib Blockrenderer to render a partial view to a string
    primarykey
    data
    text
    <p>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.</p> <p>This is causing me a headache from these two problems:</p> <ol> <li>ObjectViewData - I can't figure out what that is</li> <li>RenderPartial is a sub, but seems to be used as a function - I don' get it</li> </ol> <p>I reference the MVCContrib.UI, so I don't need to convert that. But these two functions, I do need to convert:</p> <p>(from <a href="http://www.brightmix.com/blog/how-to-renderpartial-to-string-in-asp-net-mvc/" rel="nofollow noreferrer">brightmix.com</a>)</p> <pre><code>/// 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( () =&gt; 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(); } </code></pre> <p>Here is my attempt at converting it to VB.Net</p> <pre><code>'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 </code></pre> <p><a href="http://i43.tinypic.com/2cg1ves.png" rel="nofollow noreferrer">alt text http://i43.tinypic.com/2cg1ves.png</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload