asp.net-mvc - T4MVC 不支持 DisplayTemplates 和 EditorTemplates

标签 asp.net-mvc t4mvc editorfor viewusercontrol

当我在我的 View 中使用以下内容时,我注意到了这一点:

<% Html.RenderPartial(MVC.Shared.Views.EditorTemplates.ClientOnDocuments); %>

上面的行仅返回 View 的名称,因此在本例中为ClientOnDocuments。然后,默认 View 引擎启动并尝试在当前 View 的文件夹和共享文件夹中查找 ClientOnDocuments.ascx,但不在 DisplayTemplatesEditorTemplates 中查找> 文件夹。

由于我对 T4MVC 的使用已经很深入了,所以我不想转储它或混合不同样式的引用 View (例如,如果我们提供模板的路径,上面的方法就有效)。

原因在于 T4MVC 生成的这段代码:

    public class ViewNames {
    ...
        public readonly string FirmHeader = "~/Views/Shared/FirmHeader.ascx";
        public readonly string PostsSelector = "~/Views/Shared/PostsSelector.ascx";
        static readonly _DisplayTemplates s_DisplayTemplates = new _DisplayTemplates();
        public _DisplayTemplates DisplayTemplates { get { return s_DisplayTemplates; } }
        public partial class _DisplayTemplates{
            public readonly string ClientOnDocuments = "ClientOnDocuments";
            public readonly string DateTime = "DateTime";
        }
        static readonly _EditorTemplates s_EditorTemplates = new _EditorTemplates();
        public _EditorTemplates EditorTemplates { get { return s_EditorTemplates; } }
        public partial class _EditorTemplates{
            public readonly string ClientOnDocuments = "ClientOnDocuments";
            public readonly string DateTime = "DateTime";
            public readonly string PostCode = "PostCode";
        }

您可以看到,使用共享根目录中包含的 View 一切都很好,但显然它不能很好地处理子文件夹。

我知道我可以更改 T4MVC 模板文件,但实际上希望来自 David Ebbo 的响应关于他是否会改变/纠正这一点。

希望他也能这么做,至少我十二月在这里见过他。

最佳答案

有趣的是,这种不同的行为是在另一个用户遇到问题后故意引入的。在 T4MVC.settings.t4 中查找此内容:

// Views in DisplayTemplates and EditorTemplates folders shouldn't be fully qualifed as it breaks
// the templated helper code
readonly string[]  NonQualifiedViewFolders = new string[] {
  "DisplayTemplates",
  "EditorTemplates"
};

通常情况下,子文件夹会获得完整路径,但只有这两个文件夹不会。

我认为区别在于用户调用 DisplayFor/EditorFor 来渲染它们,而您调用 RenderPartial。

无论如何,由于这是在设置文件中而不是主模板中,因此如果您不想要这种行为,您可以简单地更改列表,即

readonly string[]  NonQualifiedViewFolders = new string[] { };

希望这有帮助! :)

关于asp.net-mvc - T4MVC 不支持 DisplayTemplates 和 EditorTemplates,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4869761/

相关文章:

c# - 没有 Controller 的 Web API MVC 模型验证

asp.net-mvc - Aspnet Core Azure 失败并出现 HTTP 错误 502.5 - 进程失败

asp.net-mvc - MVC 与 WebForms

javascript - 对于外部 JS 文件有类似 T4MVC 的解决方案吗?

c# - 如何让 T4MVC 重新生成 .generated.cs 文件?

asp.net-mvc - EditorFor ListView 错误

c# - 缺少程序集引用错误显示,但引用已存在

c# - 构建 MVC3 EditorFor 模板时有没有办法访问 DataAnnotations?

asp.net-mvc-3 - 在 mvc3 中使 Html.Editorfor 字段只读

c# - 在 View 中生成新 anchor 时,我可以强制 T4MVC 不要从当前 View 的 URL 中获取参数吗?