Orsi
Orsi

Reputation: 575

Sending emaills with template MVC using Razor

I want to send some mails from my site.

I've created a template: OrderPlacedEmail.cshtml

@model OnlineCarStore.Models.PurchaseVM

<h1>Order Placed Email Notification</h1>
<p>@Model.Comments</p>

 Dear @Model.Name,

 <h2>Thank you.</h2>
<p>
You’ve made a purchase on <a href="">@Model.Comments</a>
</p>....and so on...

I've created a view model, and I use it like this:

 var template = Server.MapPath("~/Templates/OrderPlaced.cshtml");
 var viewModel = new PurchaseVM
 {
     GuId = new Guid(guidValue),
     Name = name,
     Address = address,
     Phone = phone,
     Email = email,
     Comments = comments,
     Date = DateTime.Now,
     CartList = cartList
 };

 var body = Razor.Parse(template, viewModel);

As I understood, the Razor.Parse method, should replace all the details from my template with the values from view model. But, the body gets the value of the location of the template, as you can see below:

enter image description here Can you please advise what I'm doing wrong.

Upvotes: 2

Views: 262

Answers (2)

You also can use ActionMailerNext lib from NuGet Gallery for this scenario.

public class EmailController : MailerBase
{
//...
    public EmailResult OrderPlaced(Order order)
    {
        MailAttributes.To.Add(new MailAddress("[email protected]"));
        MailAttributes.From = new MailAddress("[email protected]");

        return Email("OrderPlaced", new PurchaseVM
        {
           //...
        });
    }
//...
}

You can leave your View unchanged.

Upvotes: 1

fatih.aslantas
fatih.aslantas

Reputation: 46

If you wish there is a helper that i use

public static class HtmlOutputHelper
{

    public static string RenderViewToString(ControllerContext context,
                                string viewPath,
                                object model = null,
                                bool partial = false)
    {
        // first find the ViewEngine for this view
        ViewEngineResult viewEngineResult = null;
        if (partial)
            viewEngineResult = ViewEngines.Engines.FindPartialView(context, viewPath);
        else
            viewEngineResult = ViewEngines.Engines.FindView(context, viewPath, null);

        if (viewEngineResult == null)
            throw new FileNotFoundException("View cannot be found.");

        // get the view and attach the model to view data
        var view = viewEngineResult.View;
        context.Controller.ViewData.Model = model;

        string result = 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();
        }

        return result;
    }
}

On your controller

var viewModel = new PurchaseVM
 {
     GuId = new Guid(guidValue),
     Name = name,
     Address = address,
     Phone = phone,
     Email = email,
     Comments = comments,
     Date = DateTime.Now,
     CartList = cartList
 };

var emailTemplate = "~/Views/Templates/OrderPlaced.cshtml";
var emailOutput = HtmlOutputHelper.RenderViewToString(ControllerContext, emailTemplate, emailModel, false);

Upvotes: 1

Related Questions