且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在编辑模板调用另一个模板编辑器使用相同型号

更新时间:2023-02-11 19:17:09

简短的回答:

使用 Html.Partial 代替。

因此​​,在你Template1.cshtml文件:

So, in your Template1.cshtml file:

@model foo

// insert code here to edit the default fields.

// display extra fields via another editor template.
@Html.Partial("EditorTemplates/Template2", Model)

龙答:

这可悲的似乎是特意设计的。 MVC跟踪已呈现的模型,如果你的模型已经由一个模板渲染,它​​不会做两次,即使模板是不同的。因此,为什么第二次 @ Html.EditorForModel(则Template2)只是什么都不做。

This sadly appears to be by-design. MVC tracks the models that have been rendered, and if your model has already been rendered by a template, it won't do it twice, even if the template is different. Hence why the second @Html.EditorForModel("Template2") just does nothing.

具体地讲,它是跟踪在 ViewData.TemplateInfo.VisitedObjects ,它是一个内部字段,所以有在你没有希望的事实之后修改它。这一领域的意图是prevent无限递归。高贵,但恼人的,因为它并不需要使用到的模板。

Specifically, it's tracked in ViewData.TemplateInfo.VisitedObjects, which is an internal field, so there's no hope in you modifying it after the fact. The intention of this field is to prevent infinite recursion. Noble, but annoying in that it doesn't take the template used into account.

我发现了这一点,通过查看源$ C ​​$ C: HTTP://aspnetwebstack.$c$cplex.com/
这是伟大的发现MVC的这些奇怪的特质。

I found this out by looking at the source code: http://aspnetwebstack.codeplex.com/, which is great for finding these weird idiosyncrasies of MVC.