0
I took a code right here in the stack to render a view to string.
When the view to be converted is in the same context, that is, in the same controller folder works normally, however if I try to render a view that is in another controller does not work.
Does anyone have any idea how to render a view that is in 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();
}
}