I've got a code right here on the stack to render a view to string.
When the view to be converted is in the same context ie in the same folder of the controller it works normally, but if I try to render a view that is in another controller does not work.
Does anyone have any idea how I can render a view that is on another Controller?
public string RenderRazorViewToString(string viewName, object model)
{
//aqui mudei o codigo para tentar chamar um Controller fora do contexto atual, mas não rolou
Controller controller = new PedidoController();
controller.ViewData.Model = model;
using (var sw = new StringWriter())
{
var viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);
var viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
viewResult.View.Render(viewContext, sw);
viewResult.ViewEngine.ReleaseView(controller.ControllerContext, viewResult.View);
return sw.GetStringBuilder().ToString();
}
}