在编辑模板调用另一个模板编辑器使用相同型号 [英] In an Editor Template call another Editor Template with the same Model

查看:139
本文介绍了在编辑模板调用另一个模板编辑器使用相同型号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个编辑模板和编辑模板中我想打电话给同型号(即嵌套)其他编辑器模板,但它似乎并没有显示。结果
IE浏览器。 \\ EditorTemplates \\ Template1.cshtml

I have an editor template and within that editor template i want to call another editor template with the same model (i.e. nested), but it does not seem to display.
ie. \EditorTemplates\Template1.cshtml

@model foo

// insert code here to edit the default fields.

// display extra fields via another editor template.
@Html.EditorForModel("Template2")   // or @Html.EditorFor(m => m, "Template2")

和\\ EditorTemplates \\ Template2.cshtml

and \EditorTemplates\Template2.cshtml

@model foo

@Html.TextBoxFor(m => m.Name)

我相信有人会质疑,为什么?那么,嵌套模板将只有当条件满足显示(即@if(@ Model.IsConditionMet){....}),但我已经离开了我的原型简单。

I am sure someone will question why? Well, the nested template will only be displayed if a condition is met (ie. @if (@Model.IsConditionMet) { .... } ), but I have left that out of my prototype for simplicity.

推荐答案

简短的回答:

使用 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.

这篇关于在编辑模板调用另一个模板编辑器使用相同型号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆