这是一个奇怪的问题。我有以下视图文件(Views/Search/Submit.cshtml
):
@model IEnumerable<KeyValuePair<string, ISearchProvider>>
@foreach (var provider in Model)
{
var results = provider.Value.Results.Take(10);
if (results.Count() > 0)
{
<text><li class="dropdown-header">@provider.Key</li></text>
@Html.DisplayFor(x => results)
}
}
... 哪里results
是System.Collections.Generic.IEnumerable<out T>
和T is ISearchMatch
。
然后我在中定义了一个显示模板Views/Search/DisplayTemplates/SiteSearchMatch.cshtml
;
@model SiteSearchMatch
<li>@Html.ActionLink(Model.Name, "details", "site", new { Id = Model.Id }, null)</li>
...并像这样SiteSearchMatch
实现ISearchMatch
;
public class SiteSearchMatch: ISearchMatch
{
public int Id { get; set; }
public string Name { get; set; }
}
我希望我的显示模板被使用;但事实并非如此。相反,我看到的输出是;
<li class="dropdown-header">sites</li>
11147166811481897189813271028
...其中那串数字是我想通过显示模板呈现的所有Id
s 的组合。ISearchMatch
似乎 Razor 只是ISearchMatch
使用类中定义的第一个属性来呈现;如果我删除Id
属性的定义,我会看到所有Name
' 的组合ISearchMatch
。
有谁知道为什么会这样,以及如何让 Razor 使用我指定的显示模板?