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.