Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

How to set up the Tags in the editor to select different controllers to render?

H F
H F
Vote:
 

Hello everyone,

I have a page type and I would like the instances of it to have different views/rendered differently.

As far as I understand I can do it by creating a several controllers for the page type and using TemplateDescriptor with specific TAG.

I know that I can select specific controller while making a call from a view to a partial view, but i need to set the tag somewhere on the page instance.

My question is, how can I specify at the time of page type instance creation which controller it should use for the rendering?

One way, I guess, it to create DisplayOptionsInitializationModule. But in this way all the page type instances will have those options. 

Thank you for any hints and suggestions!

HF

#200731
Jan 23, 2019 16:38
Vote:
 

"page type instance creation" - you mean when editor is creating new page?

#200735
Jan 23, 2019 23:04
H F
Vote:
 

I mean,

yes, exactly.

let's say i have a property on that page class called 'ViewName'. Then when I create an instance of this class I pick up the ViewName to use for this instance, let it be 'simple list'. The name can be one of the tags I later on use for TemplateDesriptor for the controllers of that page class.

And at this point I am lost.

How to connect that page property to the specific controller/view with that TemplateDescriptor attribute. Is it even possible?

I know from the site example, they use the blocks types with different controllers/views via calling it from the page view specifying the tag... 

But my question is - is it possible to have different views for a page class?

Hopefully I explained it better this time.

thank you

#200736
Jan 23, 2019 23:15
Vote:
 

Hi H F,

To mee it sounds like you would like to have different rendering for the whole page based on what the editor has selected as the "page rendering template" in a property of the PageData of your implementation. I think you might be confusing the rendering tag used in content areas here where you could have different tags on different content areas and the content would be rendered differently based on the used tag(s) on a content area.

In your case it might be enough to just use a simple approach. In your page type controller index action you check the passed in page data models "view template" value and based on that use a different view file to render the page.

Simple pseudo code:

        public ViewResult Index(MyPageData currentPage)
        {
            var viewModel = new MyViewModel(currentPage);

            string viewName = "~/Views/MyPageData/Index.cshtml";

            if (!string.IsNullOrWhiteSpace(currentPage.EditorSelectedViewName))
            {
                viewName = $"~/Views/MyPageData/{currentPage.EditorSelectedViewName}.cshtml";
            }

            return View(viewName, viewModel);
        }

And naturally you could have a different view model per view but main point being that you would have one page controller and the index action contains the logic to select different view file to be used on what the editor has selected in the contents property.

You could also use the display channels to select the controller (the tag way) but it really is more tied to a device or the HttpContext rather that the properties on a content type instance. For reference a blog post by Eric Herlitz, selecting different display channels based on device.

#200747
Jan 24, 2019 8:09
Vote:
 

if I may ask - what's use case for editors to select rendering template?

#200751
Jan 24, 2019 8:57
H F
Vote:
 

@Valdis:

the use case is to use the page class and have the view in one case with the facets, the other view - just a simple list (besides, the data sources are different for the views). 

I understand that I can do something on the view side and do the check and select specific block of cshtml. 

But I wanted a clean approach - to have different controllers/views for the SAME class, and give control to the editors to select which one to use.

@Antti:

yes, this is what have used for now - created a switch in the Index method and based on the parameter in the page property I am selecting different views with appropriate viewmodel. 

yes, you are right, from what i have read, the tags are used in the contentareas for the blocks and pages with the partial controllers and partial views. 

My question was if I could use TemplateDescripter at the controller for the whole page to select different view to render or it is strickly the way to use with the partial controllers for the instances dropped into the content areas. Seems like just for the partial controllers.

Thank you guys for your comments and help. Will stick with the swich in the class controller for now.

#200781
Jan 24, 2019 15:58
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.