0

我有一些具有不可預知屬性的對象,我想使用Mustache.js在電子郵件中呈現HTML內容以將其發送給我的用戶電子郵件。這是我的代碼:在電子郵件內容的渲染HTML中使用Mustache.js

在我的HomeController

public ActionResult Index() 
{ 
    Person person = new Person 
           { 
            Name = "Dona", 
            Email = "[email protected]", 
           }; 
    PartialViewResult path = PartialView("PartialViewEmail"); 
    string tmp = path.ViewName; 
    string resultRenderHtmlEmail = RenderRazorViewToString(tmp, person); 

    return View(); 
} 

public ActionResult PartialViewEmail(Person model) 
{ 
    return PartialView(model); 
} 

public string RenderRazorViewToString(string viewName, object model) 
{ 
    ViewData.Model = model; 
    using (var sw = new StringWriter()) 
    { 
     var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName); 
     var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw); 
     viewResult.View.Render(viewContext, sw); 
     viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View); 
     return sw.GetStringBuilder().ToString(); 
    } 
} 

這是我PartialViewEmal.cshtml

@model MvcApplication1.Models.Person 

<script src="~/Scripts/mustache.js"></script> 
<h1>This is razor partial view that demo for body of email</h1> 
I'm @Model.Name and I'm @Model.Email years old. 
<div id="sampleArea"> 
</div> 
<script type="text/javascript"> 
    $(function() { 
     var data = { 
      employees: [ 
      { 
       firstName: "Christophe", 
       lastName: "Coenraets" 
      }, 
      { 
       firstName: "John", 
       lastName: "Smith" 
      } 
      ] 
     }; 
     var template = "Employees:<ul>{{#employees}}" + 
            "<li>{{firstName}} {{lastName}}</li>" + 
            "{{/employees}}</ul>"; 
     var html = Mustache.to_html(template, data); 
     $('#sampleArea').html(html); 
    }); 
</script> 

這是resultRenderHtmlEmail結果:

<script src="/Scripts/mustache.js"></script> 
<h1>This is razor partial view that demo for body of email</h1> 
I'm Dona and I'm [email protected] years old. 
<div id="sampleArea"> 
</div> 
<script type="text/javascript"> 
    $(function() { 
     var data = { 
      employees: [ 
      { 
       firstName: "Christophe", 
       lastName: "Coenraets" 
      }, 
      { 
       firstName: "John", 
       lastName: "Smith" 
      } 
      ] 
     }; 
     var template = "Employees:<ul>{{#employees}}" + 
            "<li>{{firstName}} {{lastName}}</li>" + 
            "{{/employees}}</ul>"; 
     var html = Mustache.to_html(template, data); 
     $('#sampleArea').html(html); 
    }); 
</script> 

一點也沒有」不瞭解mustache.js!

我該怎麼辦?

非常感謝!

+0

有人可以幫助我! :) –

回答

1

正如你所知道的JavaScript代碼在客戶端執行。但郵件客戶端和郵件服務將不支持電子郵件中的可執行代碼。

另外剃刀視圖引擎不支持執行js。所以你不能用任何js模板模板給你發電子郵件。

+0

服務器@ YD1m中的Nustache.NET如何? –

相關問題