Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Zakhar 171 posts 397 karma points
    May 01, 2014 @ 13:25
    Zakhar
    0

    Render view result as a string for emailing

    Hi guys,

    I've set up a document type and an Umbraco template for use for email templates on my site. Now I basically have a page which html I need to read and use in emails.

    I found a helper which presumably does half of the work (couldn't test it yet though) here: http://our.umbraco.org/wiki/reference/code-snippets/razor-snippets/render-razor-scripts-for-emails-and-more/create-string-of-partial-view-for-emailing

    public static string RenderPartialViewToString(Controller controller, string viewName, object model)
        {
            controller.ViewData.Model = model;
            try
            {
                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.GetStringBuilder().ToString();
                }
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }
    

    This method accepts a controller as a parameter, but I can't figure out how to create the controller I need.

    I guess it's not the only way to do it, can anyone advice on it? Any help will be greatly appreciated.

    Thank you.

    P.S. I use Umbraco 6.1.6

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies