予測できないプロパティを持つオブジェクトがいくつかあり、Mustache.js を使用して HTML コンテンツを電子メールでレンダリングし、ユーザーの電子メールに送信したいと考えています。そして、これは私のコードです:
私のホームコントローラーで
public ActionResult Index()
{
Person person = new Person
{
Name = "Dona",
Email = "dona3315@gmail.com",
};
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 dona3315@gmail.com 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 を理解していません。
私は何をすべきか ?
本当にありがとう !