Have you ever needed to render your ASP.NET partial view as a string?  For example you have to replace a predefined string by the result of your partial view?  Well, I found a great function to render your partial view as a string in C#, and quickly converted it to VB.NET.  Thanks to Kevin for the C# code :)

Public Function RenderPartialToString(ByVal controlName As String, ByVal viewData As Object) As String

        Dim vd As New ViewDataDictionary(viewData)
        Dim vp As New ViewPage()
        vp.ViewData = vd
        Dim control = vp.LoadControl(controlName)

        vp.Controls.Add(control)

        Dim sb As New StringBuilder()
        Using sw As New StringWriter(sb)
            Using tw As New HtmlTextWriter(sw)
                vp.RenderControl(tw)
            End Using
        End Using

        Return sb.ToString()
    End Function

01/14/2010 - 09:55
.NET MVC, VB.NET

If you want to save data by posting a form using ASP.NET MVC, all data is always validated so that potentially dangerous content is blocked.  But sometimes you want to let your users post HTML to the server, for example with a text editor (wysiwyg). 

 

You can allow this for a specific function withing your ASP.NET MVC Controller.  Just use this function attribute in your controller:

<ValidateInput(False)> _

12/31/2009 - 15:36
.NET, MVC