c# - How do you convert a Razor view to a string? -


i use razor view kind of template sending emails, "save" template in view, read controller string, necessary replacements, , send it.

i have solution works: template hosted somewhere html page, put application (i.e. in view). don't know how read view string in controller.

i use following. put on base controller if have one, way can access in controllers.

public static string renderpartialtostring(controller controller, string viewname, object model) {     controller.viewdata.model = model;      using (stringwriter sw = new stringwriter())     {         viewengineresult viewresult = viewengines.engines.findpartialview(controller.controllercontext, viewname);         viewcontext viewcontext = new viewcontext(controller.controllercontext, viewresult.view, controller.viewdata, controller.tempdata, sw);         viewresult.view.render(viewcontext, sw);          return sw.getstringbuilder().tostring();     } } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -