Skip to content

Instantly share code, notes, and snippets.

@simple17
Created July 11, 2020 09:11
Show Gist options
  • Save simple17/dcdb753590ebb92396ef66435b83e123 to your computer and use it in GitHub Desktop.
Save simple17/dcdb753590ebb92396ef66435b83e123 to your computer and use it in GitHub Desktop.
[Render Block into string] #episerver
using System.IO;
using System.Web.Mvc;
namespace Foundation.Helpers
{
public static class ConvertHelper
{
public static string RenderViewToString(ControllerContext context, string viewName, object model)
{
if (string.IsNullOrEmpty(viewName))
viewName = context.RouteData.GetRequiredString("action");
var viewData = new ViewDataDictionary(model);
using (var sw = new StringWriter())
{
var viewResult = ViewEngines.Engines.FindPartialView(context, viewName);
var viewContext = new ViewContext(context, viewResult.View, viewData, new TempDataDictionary(), sw);
viewResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}
}
}
var result = pages.Items
.GetRecipes()
.Select(x => ConvertHelper.RenderViewToString(ControllerContext, "~/Features/Shared/_Thumbnail.cshtml", x));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment