I've been searching the site a lot, but all I could find were examples on how to render partial controls .ascx
, or depended on a controller context.
I want a method that enables me to provide just the relative path to the view, and a model, and render that view with that model into a string:
string result = Utility.RenderViewToString("~/Views/My/Profile.cshtml", model);
All the examples I could find were either for older versions of MVC, or simply didn't do what I need to do here.
I had created solution for me. It's Extension that render view into string.
And than we can render view in Action
Though Marc's is answer is correct and works for previous versions. But that is now obselete and needs to be replaced.
Here is the new code that worked for me and you can find more useful information on Github : Razor Engine
You can achieve that with the razorengine.
And it does not rely on the controller context - but because of that you are not able to use the Html helpers (which rely on the http context). But it is perfect to use razor as a template engine for strings.
You can check this link.
extracted content From Render Razor view to String. .