Created
July 11, 2020 09:11
-
-
Save simple17/dcdb753590ebb92396ef66435b83e123 to your computer and use it in GitHub Desktop.
[Render Block into string] #episerver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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