Can i call a "shared" editor template from within a "areas" editor template, for the same model?
Asked Answered
A

2

8

I've got an editor template location in:

Areas/Posts/Views/Shared/EditorTemplates/Question.cshtml

I also have one in:

/Views/Shared/EditorTemplates/Question.cshtml

For both, the model is the same.

What i'm trying to do is within a View in the Posts area, call my editor template in the area, set some HTML and then bubble back up to the main shared editor template.

Here's the Posts EditorTemplate:

@model xxx.ViewModels.QuestionViewModel
@Html.Hidden("Id", (byte)Model.QuestionType)
@Html.EditorForModel()

But all it does it render out the hidden field, not the contents of the shared editor template.

If i get rid of the Posts editor template, the shared one is rendered properly.

I'm guessing MVC/Razor thinks this is recursive or something? Like im calling the same template?

Is there any way i can tell it to go to the shared one?

Essentially, i'm trying to re-use the HTML in the shared template, but inject some sneaky HTML of my own.

African answered 17/11, 2011 at 6:28 Comment(0)
A
2

As the HTML markup for the shared editor template was very simple (just rendered a checkbox and a label), i abstracted the markup into a custom HTML helper, then called this from both the shared template and my areas template.

~/Areas/Posts/Views/Shared/EditorTemplates/Question.cshtml:

@model xxx.ViewModels.QuestionViewModel
@Html.Hidden("Id", (byte)Model.QuestionType)
@Html.QuestionCheckBoxForModel()
@Html.QuestionLabelForModel()

~/Views/Shared/EditorTemplates/Question.cshtml:

@model xxx.ViewModels.QuestionViewModel
@Html.QuestionCheckBoxForModel()
@Html.QuestionLabelForModel()
African answered 17/11, 2011 at 23:10 Comment(3)
Any chance you could post the code for your custom HTML Helpers? This is an interesting solution.Fancywork
@Chris - i can if you like, but it's trivial. Just wraps the calls to @Html.CheckBoxFor and @Html.LabelFor, simply to neaten up the template HTML.African
@Chris - it's literally: htmlHelper.CheckBoxFor(model => model.SomeProperty) and htmlHelper.LabelFor(model => model.SomeProperty), since the custom HTML helper is typed to QuestionViewModel (e.g HtmlHelper<QuestionViewModel> htmlHelper). Looking at my code now, i ended up combining the checkbox and label helpers into one, and just calling it, e.g: @Html.CheckboxAndLabelForModel()African
B
10

You can only have 1 template used at runtime for a given type. ASP.NET MVC first looks in areas shared templates folder and since it finds a corresponding template there it picks it up and it uses it. It then stops looking and the template you put in the main shared folder is never used. It's by design.

Is there any way i can tell it to go to the shared one?

Yes, you can explicitly specify the location of the template, but then it won't use the template in your areas folder:

@Html.EditorFor(x => x.Question, "~/Views/Shared/EditorTemplates/Question.cshtml")
Batchelor answered 17/11, 2011 at 7:9 Comment(3)
You can only have 1 template used at runtime for a given type - are you sure? I have a template in one area, then another template for the same type in the shared folder. Then if im in a different area, it will use the shared folder one (since the area one is not in the current area). So i don't think that statement is true. I think it should read You can only have 1 template used at runtime for a given type, in a given Views particular context. or something similar.African
Instead of pointing to the shared one from the view, could i do something like this in my areas editor template: @Html.EditorForModel("~/Views/Shared/EditorTemplates/Question.cshtml") or it is impossible to make use of both?African
Does passing specific template @Html.EditorFor(x => x.Question, "~/Views/Shared/EditorTemplates/Question.cshtml") works for somebody? I have ASP MVC 5, and it doesn't work in my case. It understands only short names like "Answer" for "~/Views/Shared/EditorTemplates/Answer.cshtml", in other cases it falls back to area editor template.Santiagosantillan
A
2

As the HTML markup for the shared editor template was very simple (just rendered a checkbox and a label), i abstracted the markup into a custom HTML helper, then called this from both the shared template and my areas template.

~/Areas/Posts/Views/Shared/EditorTemplates/Question.cshtml:

@model xxx.ViewModels.QuestionViewModel
@Html.Hidden("Id", (byte)Model.QuestionType)
@Html.QuestionCheckBoxForModel()
@Html.QuestionLabelForModel()

~/Views/Shared/EditorTemplates/Question.cshtml:

@model xxx.ViewModels.QuestionViewModel
@Html.QuestionCheckBoxForModel()
@Html.QuestionLabelForModel()
African answered 17/11, 2011 at 23:10 Comment(3)
Any chance you could post the code for your custom HTML Helpers? This is an interesting solution.Fancywork
@Chris - i can if you like, but it's trivial. Just wraps the calls to @Html.CheckBoxFor and @Html.LabelFor, simply to neaten up the template HTML.African
@Chris - it's literally: htmlHelper.CheckBoxFor(model => model.SomeProperty) and htmlHelper.LabelFor(model => model.SomeProperty), since the custom HTML helper is typed to QuestionViewModel (e.g HtmlHelper<QuestionViewModel> htmlHelper). Looking at my code now, i ended up combining the checkbox and label helpers into one, and just calling it, e.g: @Html.CheckboxAndLabelForModel()African

© 2022 - 2024 — McMap. All rights reserved.