我有一个html模板来发送电子邮件。如何获取表标记并动态注入所有行?
html模板:
zed_board
cs file
adding /usr/local/cuda/extras/CUPTI/lib64 to your LD_LIBRARY_PATH
答案 0 :(得分:1)
这是我自己的建议。如果您正在使用MVC,请为您的电子邮件创建一个独立的视图(让我们说myemail.cs)并为其分配一个模型(myemailModel)。
这是你的myemailModel类
public class myemailModel {
public string Username{get;set;}
public IList<Data> data{get;set;}
public class Data{
public string Name{get;set;}
public int Age{get;set;}
}
}
所以你的观点看起来像这样:
@model myemailModel
<html>
<body>
Hello @Model.Email!
@if (Model.data !=null && Model.data.count() >0)
{
<table class="tblCss">
<tr>
<th> Name </th>
<th> Age </th>
</tr>
foreach (var i in Model.data)
{
<tr>
<td>@i.Name</td>
<td>@i.Age</td>
</tr>
}
</table>
}
</body>
</html>
最后,您需要将视图渲染为字符串。使用以下方法(Render a view as a string):
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();
}
}
并调用上面的方法来获取正文:
string body = RenderRazorViewToString("myemail", new myemailModel {
Username="foo",
data = new List<myemailModel>{
// fill it
}
});