ASP.NET 显示模板 - 无输出 [英] ASP.NET Display Templates - No output
问题描述
我有这个模型:
public class DefinitionListModel : List<DefinitionListItemModel>
{
}
然后我在 DisplayTemplates 中为它设置了这个显示模板(部分视图):
Then I have this display template (partial view) setup for it in DisplayTemplates:
@model Company.Product.Models.DefinitionListModel
<dl>
@foreach (var definition in Model)
{
Html.DisplayFor(m => definition);
}
</dl>
它调用这个默认显示模板,因为每个项目都是一个DefinitionListItemModel
:
Which calls this default display template because each item is a DefinitionListItemModel
:
@model Company.Product.Models.DefinitionListItemModel
@Html.DisplayFor(m => m.Term, "DefinitionTerm");
@Html.DisplayFor(m => m.Description, "DefinitionDescription");
DefinitionTerm
模板如下所示:
@model object
@{
if (Model != null)
{
string s = Model as string;
if (s != null)
{
<dt>@s</dt>
}
else
{
// Other types and models that could be used.
// Last resort.
<dt>@Model.ToString()</dt>
}
}
}
放置在最后一个模板中的断点成功命中,视图和标记似乎运行得很好,但没有渲染,甚至上面第一个模板中的 DL 标记也不渲染.
A breakpoint placed in the last template is hit successfully and the view and mark-up appears to run through just fine, but none of it renders, not even the DL tag from the first template above.
为什么它会命中断点但不呈现任何 HTML?
Why does it hit breakpoints but not render any HTML?
推荐答案
这完全与分号有关.似乎我不能像使用分号终止符的普通 C# 方法一样调用 Html.DisplayFor
.
It was all to do with semi-colons. It seems I can't just call Html.DisplayFor
like a normal C# method with a semi-colon terminator.
例如,这是修改后的 DefinitionListModel
模板:
For example, here's the revised DefinitionListModel
template:
@model AcmeCo.Hudson.Models.DefinitionListModel
@if (Model != null)
{
<dl>
@foreach (var definition in Model)
{
@Html.DisplayFor(m => definition)
}
</dl>
}
请注意 @
在已经是 C# 的代码块中的奇怪使用.我认为有一种正确的方法可以从 C# 块中执行 DisplayFor
但我不知道它是什么.
Note the bizarre use of @
within a code block that's already C#. I assume there's a proper way to do DisplayFor
from within a C# block but I don't know what it is.
这篇关于ASP.NET 显示模板 - 无输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!