0

我有以下方法根据PropertyInfo引用TDatas属性返回模型的 html 代码。

 public MvcHtmlString GetHtml(HtmlHelper<TData> helper) {

        if (PropertyInfo.IsDefined(typeof(UIHintAttribute), true)) {
            UIHintAttribute uiHintAttribute = PropertyInfo.GetCustomAttributes(typeof(UIHintAttribute), true).Cast<UIHintAttribute>().FirstOrDefault();
            if (uiHintAttribute != null) {
                // Templatename ignored here
                return helper.Display(PropertyInfo.Name, uiHintAttribute.UIHint);
            }
        }

        return helper.Display(PropertyInfo.Name);
    }

显示模板的名称通过 UIHint 定义并正确读取。
所以一个电话可能是:

return helper.Display("Name", "NameDisplayTemplate");

不幸的templatename是,当我想使用特定的 DisplayTemplate 呈现属性时,该参数被忽略。我尝试了显示模板(文件夹显示模板)和部分视图。他们都没有工作。

有人有什么想法吗?

4

1 回答 1

0

问题是模板必须放在 DisplayTemplates-Folder 中,因为它是在 msdn.xml 中编写的。

如果在控制器的 DisplayTemplates 文件夹中找到名称与 templateName 参数匹配的模板,则使用该模板来呈现表达式。如果在控制器的 DisplayTemplates 文件夹中找不到模板,则会在 Views\Shared\DisplayTemplates 中搜索与 templateName 参数名称匹配的模板。如果没有找到模板,则使用默认模板。

于 2014-04-17T11:27:51.037 回答