views:

162

answers:

1

I am building a web app which will generate lots of different code templates (HTML tables, XML documents, SQL scripts) that I want to render on screen as encoded HTML so that people can copy and paste those templates.

I would like to be able to use asp.net mvc views to generate the code for these templates (rather than, say, using a StringBuilder).

Is there a way using asp.net mvc to store the results of a rendered view into a string? Something like the following perhaps?

public ContentResult HtmlTable(string format)
{
    var m = new MyViewModel();
    m.MyDataElements = _myDataRepo.GetData();

    // Somehow render the view and store it as a string?
    // Not sure how to achieve this part.
    var viewHtml = View(m);

    var htmlEncodedView = Server.HtmlEncode(viewHtml);

    return Content(htmlEncodedView);
}

NOTE: My original question mentioned NHaml views specifically, but I realized that this wasn't view engine specific. So if you see answers related to NHaml, that's why.

A: 

Did you set the default controller factory? Take a look at http://andrewpeters.net/2007/12/19/introducing-nhaml-an-aspnet-mvc-view-engine/

stimms
Yeah. My question isn't about how to get NHaml up and running. I have it running just fine. I am trying to figure out how to render a view to a string.
jessegavin