Pass a VIEW as a STRING

255 views Asked by At

Attempting to use the example on http://weblog.west-wind.com/posts/2012/May/30/Rendering-ASPNET-MVC-Views-to-String to pass a VIEW as a string, and send as an Email. Which should send the invoice of a sale through email to the user.

Iv'e added ViewRenderer class to my project. Then added the ContactSeller function to my controller, and copied and renamed the invoice view as ViewOrderThroughEmail.cshtml

  [HttpPost]
    [AlwaysAccessible]
    public ActionResult SendEmailAttachment(QBCustomerRecord cust)
    {
        ContactSellerViewModel model = new ContactSellerViewModel();
        string invoiceEmailAsString = ContactSeller(model);
        _userService.SendEmail(username, nonce => Url.MakeAbsolute(Url.Action("LostPassword", "Account", new { Area = "Orchard.Users", nonce = nonce }), siteUrl), invoiceEmailAsString);

        _orchardServices.Notifier.Information(T("The user will receive a confirmation link through email."));

        return RedirectToAction("LogOn");
    }

    [HttpPost]
    public string ContactSeller(ContactSellerViewModel model)
    {   
        string message = ViewRenderer.RenderView("~/Orchard.Web/Modules/RainBow/Views/Account/ViewOrderThroughEmail.cshtml",model,
                                                     ControllerContext);     
        model.EntryId = 101;
        model.EntryTitle = message;


        return message;
    }

but this throws a error, VIEW cant be NULL:

  using (var sw = new StringWriter())
    {
        var ctx = new ViewContext(Context, view,
                                    Context.Controller.ViewData,
                                    Context.Controller.TempData,
                                    sw);
        view.Render(ctx, sw);
        result = sw.ToString();
    }

in the RenderViewToStringInternal function in the ViewRenderer.cs. I oringally thought it was the path of the view, but its not.

Any ideas? Thanks

1

There are 1 answers

0
dav_i On

I use the following extension method in my projects:

public static string RenderView(this Controller controller, string viewName, ViewDataDictionary viewData)
{
    var controllerContext = controller.ControllerContext;

    var viewResult = ViewEngines.Engines.FindView(controllerContext, viewName, null);

    StringWriter stringWriter;

    using (stringWriter = new StringWriter())
    {
        var viewContext = new ViewContext(
            controllerContext,
            viewResult.View,
            viewData,
            controllerContext.Controller.TempData,
            stringWriter);

        viewResult.View.Render(viewContext, stringWriter);
        viewResult.ViewEngine.ReleaseView(controllerContext, viewResult.View);
    }

    return stringWriter.ToString();
}

public static string RenderView(this Controller controller, string viewName, object model)
{
    return RenderView(controller, viewName, new ViewDataDictionary(model));
}

Then in your action method:

var viewString = this.RenderView("ViewName", model);