Does anybody know how to get the generated html of a view inside an action?
Is it something like this:
public ActionResult Do() { var html = RenderView("hello", model); ... }
You can use the Html. Raw() method for that.
ASP.NET MVC4 Web API controller should return Razor view as html in json result property. Message=The method or operation is not implemented. var viewResult = ViewEngines.
To return a view from the controller action method, we can use View() method by passing respective parameters. return View(“ViewName”) – returns the view name specified in the current view folder (view extension name “. cshtml” is not required. return View("~/Views/Account/Register.
Razor is a markup syntax for embedding . NET based code into webpages. The Razor syntax consists of Razor markup, C#, and HTML. Files containing Razor generally have a . cshtml file extension.
I use a static method in a class I called Utilities.Common
I pass views back to the client as properties of JSON objects constantly so I had a need to render them to a string. Here ya go:
public static string RenderPartialViewToString(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.ToString(); } }
This will work for full views as well as partial views, just change ViewEngines.Engines.FindPartialView
to ViewEngines.Engines.FindView
.
The accepted answer by @Chev above is good, but I wanted to render the result of a specific action, not just a particular view.
Also, I needed to be able to pass parameters to that action rather than rely on injecting a model.
So I came up with my own method, that I put in the base class of my controllers (making it available to them all):
protected string RenderViewResultAsString(ViewResult viewResult) { using (var stringWriter = new StringWriter()) { this.RenderViewResult(viewResult, stringWriter); return stringWriter.ToString(); } } protected void RenderViewResult(ViewResult viewResult, TextWriter textWriter) { var viewEngineResult = this.ViewEngineCollection.FindView( this.ControllerContext, viewResult.ViewName, viewResult.MasterName); var view = viewEngineResult.View; try { var viewContext = new ViewContext( this.ControllerContext, view, this.ViewData, this.TempData, textWriter); view.Render(viewContext, textWriter); } finally { viewEngineResult.ViewEngine.ReleaseView(this.ControllerContext, view); } }
Suppose I have an action called Foo
that takes a model object and some other parameters, which together influence what view will be used:
public ViewResult Foo(MyModel model, int bar) { if (bar == 1) return this.View("Bar1"); else return this.View("Bar2", model); }
Now, if I want to get the result of calling action Foo
, I can simply get the ViewResult
by invoking the Foo
method, and then call RenderViewResultAsString
to get the HTML text:
var viewResult = this.Foo(model, bar); var html = this.RenderViewResultAsString(viewResult);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With