In an Editor Template call another Editor Template with the same Model











up vote
26
down vote

favorite
6












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. EditorTemplatesTemplate1.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")


and EditorTemplatesTemplate2.cshtml



@model foo

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


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.










share|improve this question
























  • I'm trying to do something similar, but to no avail. Any luck since you wrote this?
    – kdawg
    Nov 8 '11 at 22:03










  • Actually, follow-up info: I'm trying to do this in conjunction with Telerik's MVC grid control. This may have something to do with it too.
    – kdawg
    Nov 8 '11 at 22:10










  • @kdawg - No, no luck sorry. I have moved onto JQuery templates and Knockoutjs.
    – tkerwood
    Nov 9 '11 at 1:27












  • Hah! I'm using knockout.js in the wizard control that has said Telerik grid control. Love that library. Thanks for the response.
    – kdawg
    Nov 9 '11 at 19:47










  • related/duplicate: stackoverflow.com/questions/12192225/…
    – Goran Obradovic
    Sep 6 '12 at 5:56















up vote
26
down vote

favorite
6












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. EditorTemplatesTemplate1.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")


and EditorTemplatesTemplate2.cshtml



@model foo

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


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.










share|improve this question
























  • I'm trying to do something similar, but to no avail. Any luck since you wrote this?
    – kdawg
    Nov 8 '11 at 22:03










  • Actually, follow-up info: I'm trying to do this in conjunction with Telerik's MVC grid control. This may have something to do with it too.
    – kdawg
    Nov 8 '11 at 22:10










  • @kdawg - No, no luck sorry. I have moved onto JQuery templates and Knockoutjs.
    – tkerwood
    Nov 9 '11 at 1:27












  • Hah! I'm using knockout.js in the wizard control that has said Telerik grid control. Love that library. Thanks for the response.
    – kdawg
    Nov 9 '11 at 19:47










  • related/duplicate: stackoverflow.com/questions/12192225/…
    – Goran Obradovic
    Sep 6 '12 at 5:56













up vote
26
down vote

favorite
6









up vote
26
down vote

favorite
6






6





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. EditorTemplatesTemplate1.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")


and EditorTemplatesTemplate2.cshtml



@model foo

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


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.










share|improve this question















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. EditorTemplatesTemplate1.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")


and EditorTemplatesTemplate2.cshtml



@model foo

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


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.







asp.net-mvc razor nested editortemplates display-templates






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 3 '14 at 1:14









Simon Halsey

5,02011531




5,02011531










asked Jul 25 '11 at 4:12









tkerwood

1,4681423




1,4681423












  • I'm trying to do something similar, but to no avail. Any luck since you wrote this?
    – kdawg
    Nov 8 '11 at 22:03










  • Actually, follow-up info: I'm trying to do this in conjunction with Telerik's MVC grid control. This may have something to do with it too.
    – kdawg
    Nov 8 '11 at 22:10










  • @kdawg - No, no luck sorry. I have moved onto JQuery templates and Knockoutjs.
    – tkerwood
    Nov 9 '11 at 1:27












  • Hah! I'm using knockout.js in the wizard control that has said Telerik grid control. Love that library. Thanks for the response.
    – kdawg
    Nov 9 '11 at 19:47










  • related/duplicate: stackoverflow.com/questions/12192225/…
    – Goran Obradovic
    Sep 6 '12 at 5:56


















  • I'm trying to do something similar, but to no avail. Any luck since you wrote this?
    – kdawg
    Nov 8 '11 at 22:03










  • Actually, follow-up info: I'm trying to do this in conjunction with Telerik's MVC grid control. This may have something to do with it too.
    – kdawg
    Nov 8 '11 at 22:10










  • @kdawg - No, no luck sorry. I have moved onto JQuery templates and Knockoutjs.
    – tkerwood
    Nov 9 '11 at 1:27












  • Hah! I'm using knockout.js in the wizard control that has said Telerik grid control. Love that library. Thanks for the response.
    – kdawg
    Nov 9 '11 at 19:47










  • related/duplicate: stackoverflow.com/questions/12192225/…
    – Goran Obradovic
    Sep 6 '12 at 5:56
















I'm trying to do something similar, but to no avail. Any luck since you wrote this?
– kdawg
Nov 8 '11 at 22:03




I'm trying to do something similar, but to no avail. Any luck since you wrote this?
– kdawg
Nov 8 '11 at 22:03












Actually, follow-up info: I'm trying to do this in conjunction with Telerik's MVC grid control. This may have something to do with it too.
– kdawg
Nov 8 '11 at 22:10




Actually, follow-up info: I'm trying to do this in conjunction with Telerik's MVC grid control. This may have something to do with it too.
– kdawg
Nov 8 '11 at 22:10












@kdawg - No, no luck sorry. I have moved onto JQuery templates and Knockoutjs.
– tkerwood
Nov 9 '11 at 1:27






@kdawg - No, no luck sorry. I have moved onto JQuery templates and Knockoutjs.
– tkerwood
Nov 9 '11 at 1:27














Hah! I'm using knockout.js in the wizard control that has said Telerik grid control. Love that library. Thanks for the response.
– kdawg
Nov 9 '11 at 19:47




Hah! I'm using knockout.js in the wizard control that has said Telerik grid control. Love that library. Thanks for the response.
– kdawg
Nov 9 '11 at 19:47












related/duplicate: stackoverflow.com/questions/12192225/…
– Goran Obradovic
Sep 6 '12 at 5:56




related/duplicate: stackoverflow.com/questions/12192225/…
– Goran Obradovic
Sep 6 '12 at 5:56












1 Answer
1






active

oldest

votes

















up vote
50
down vote



+100










Short answer:



Use Html.Partial instead.



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)


Long answer:



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.



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.



I found this out by looking at the source code, which is great for finding these weird idiosyncrasies of MVC.






share|improve this answer



















  • 3




    Very helpful answer, thanks for the research!
    – Peter Gluck
    Jul 16 '14 at 23:00






  • 2




    This should be marked as the answer. Strong work.
    – akousmata
    Apr 2 '15 at 16:37






  • 1




    It's a shame we can't vote to mark as answer. Thanks for your research.
    – Ravendarksky
    Mar 6 '16 at 21:02










  • This answer should be accepted
    – Mike A
    Apr 18 at 17:19











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6811717%2fin-an-editor-template-call-another-editor-template-with-the-same-model%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
50
down vote



+100










Short answer:



Use Html.Partial instead.



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)


Long answer:



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.



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.



I found this out by looking at the source code, which is great for finding these weird idiosyncrasies of MVC.






share|improve this answer



















  • 3




    Very helpful answer, thanks for the research!
    – Peter Gluck
    Jul 16 '14 at 23:00






  • 2




    This should be marked as the answer. Strong work.
    – akousmata
    Apr 2 '15 at 16:37






  • 1




    It's a shame we can't vote to mark as answer. Thanks for your research.
    – Ravendarksky
    Mar 6 '16 at 21:02










  • This answer should be accepted
    – Mike A
    Apr 18 at 17:19















up vote
50
down vote



+100










Short answer:



Use Html.Partial instead.



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)


Long answer:



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.



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.



I found this out by looking at the source code, which is great for finding these weird idiosyncrasies of MVC.






share|improve this answer



















  • 3




    Very helpful answer, thanks for the research!
    – Peter Gluck
    Jul 16 '14 at 23:00






  • 2




    This should be marked as the answer. Strong work.
    – akousmata
    Apr 2 '15 at 16:37






  • 1




    It's a shame we can't vote to mark as answer. Thanks for your research.
    – Ravendarksky
    Mar 6 '16 at 21:02










  • This answer should be accepted
    – Mike A
    Apr 18 at 17:19













up vote
50
down vote



+100







up vote
50
down vote



+100




+100




Short answer:



Use Html.Partial instead.



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)


Long answer:



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.



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.



I found this out by looking at the source code, which is great for finding these weird idiosyncrasies of MVC.






share|improve this answer














Short answer:



Use Html.Partial instead.



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)


Long answer:



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.



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.



I found this out by looking at the source code, which is great for finding these weird idiosyncrasies of MVC.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 14 at 3:13









Stephen Muecke

97.5k1994118




97.5k1994118










answered Feb 5 '14 at 15:22









Ber'Zophus

3,52031320




3,52031320








  • 3




    Very helpful answer, thanks for the research!
    – Peter Gluck
    Jul 16 '14 at 23:00






  • 2




    This should be marked as the answer. Strong work.
    – akousmata
    Apr 2 '15 at 16:37






  • 1




    It's a shame we can't vote to mark as answer. Thanks for your research.
    – Ravendarksky
    Mar 6 '16 at 21:02










  • This answer should be accepted
    – Mike A
    Apr 18 at 17:19














  • 3




    Very helpful answer, thanks for the research!
    – Peter Gluck
    Jul 16 '14 at 23:00






  • 2




    This should be marked as the answer. Strong work.
    – akousmata
    Apr 2 '15 at 16:37






  • 1




    It's a shame we can't vote to mark as answer. Thanks for your research.
    – Ravendarksky
    Mar 6 '16 at 21:02










  • This answer should be accepted
    – Mike A
    Apr 18 at 17:19








3




3




Very helpful answer, thanks for the research!
– Peter Gluck
Jul 16 '14 at 23:00




Very helpful answer, thanks for the research!
– Peter Gluck
Jul 16 '14 at 23:00




2




2




This should be marked as the answer. Strong work.
– akousmata
Apr 2 '15 at 16:37




This should be marked as the answer. Strong work.
– akousmata
Apr 2 '15 at 16:37




1




1




It's a shame we can't vote to mark as answer. Thanks for your research.
– Ravendarksky
Mar 6 '16 at 21:02




It's a shame we can't vote to mark as answer. Thanks for your research.
– Ravendarksky
Mar 6 '16 at 21:02












This answer should be accepted
– Mike A
Apr 18 at 17:19




This answer should be accepted
– Mike A
Apr 18 at 17:19


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6811717%2fin-an-editor-template-call-another-editor-template-with-the-same-model%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Create new schema in PostgreSQL using DBeaver

Deepest pit of an array with Javascript: test on Codility

Costa Masnaga