How to html encode the output of an MVC view?
Posted
by jessegavin
on Stack Overflow
See other posts from Stack Overflow
or by jessegavin
Published on 2010-03-20T23:12:36Z
Indexed on
2010/06/14
3:32 UTC
Read the original article
Hit count: 345
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.
© Stack Overflow or respective owner