当我在视图中使用以下内容时,我注意到了这一点:
<% Html.RenderPartial(MVC.Shared.Views.EditorTemplates.ClientOnDocuments); %>上面的代码行只返回视图的名称,因此在本例中为ClientOnDocuments。然后,默认视图引擎启动并尝试在当前视图的文件夹和共享文件夹中查找ClientOnDocuments.ascx,但不在DisplayTemplates和EditorTemplates文件夹中查找。
因为我对T4MVC的使用已经走得很远了,所以我不想转储它或混合不同样式的引用视图(例如,如果我们提供模板的路径,上面的方法就会起作用)。
原因在于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";
}您可以看到,使用包含在共享根目录中的视图,一切都很好,但显然它不能很好地处理子文件夹。
我知道我可以更改T4MVC模板文件,但实际上希望得到David Ebbo的回复,说明他是否要更改/更正这个文件。
希望他能这样做,至少我去年12月在这里见过他。
发布于 2011-02-03 07:59:52
有趣的是,这种不同的行为是在另一个用户遇到问题后故意放入的。在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[] { };希望这能有所帮助!:)
https://stackoverflow.com/questions/4869761
复制相似问题