我正在尝试使用剃刀引擎在我的C#项目中创建一个模板。模板应该由我从数据库中获得的值填充。该模板如下所示
<table>
<tr>
<td> Tier : </td>
<td> @Model.tier</td>
</tr>
<tr>
<td> Author : </td>
<td> @Model.author</td>
</tr>
<tr>
<td> Quotation For : </td>
<td> @Model.quotationFor</td>
</tr>
</table>后端代码如下所示
var model = new List<KeyValuePair<string, object>>();
for (int i=0;i<value.Count;i++) {
model.Add(new KeyValuePair<string, object>(name[i], value[i]));
}
var template = File.ReadAllText(HttpContext.Current.Server.MapPath("Templates/dataDump.html"));
var body = Razor.Parse(template, model);模型列表由我从DB获取的值组成。我想使用此模型填充模板,但得到以下错误
Exception thrown: 'RazorEngine.Templating.TemplateCompilationException' in RazorEngine.dll发布于 2016-03-10 10:29:15
看起来您正在尝试传递一个非常复杂的模型,而您在模板中没有很好地定义该模型
应该是这样的。
var body = Razor.Parse(template, new {tier = "tier", author="author",quotationFor=".quotationFor"});如果你想让它像你展示的那样动态化
var model = new List<KeyValuePair<string, object>>();您必须修改模板
<table>
@foreach(var item in Model)
{
<tr>
<td>@item.Key </td>
<td>@item.Value</td>
</tr>
}
</table>https://stackoverflow.com/questions/35905946
复制相似问题