What is the convention for using a custom ViewModel in a Content-{x}.cshtml file? #17602
-
Hi there, I was wondering if there are any good practices / conventions for using a ViewModel in your Content-{x}.cshtml file where x of course can be anything like a Content-Page.cshtml for example. I made a controller used to implement a paging system and a corresponding ViewModel, but just doing @model ViewModel in the Content-Page.cshtml of course overwrites the model, which makes us lose access to the Page properties. So what is the convention to do here? I tried adding a ContentItem field to the ViewModel and getting the ContentItem using Razor helpers (query/alias) to pass the contentitem I want on but this doesn't work. I got errors for this yesterday I'll update this post after trying again to see what errors I got exactly UPDATE: The errors I got are because the model that is passed on is of type ZoneHolding, I am assuming this causes errors because OrchardCore expects a particular model it bounds to ContentItem-{x}.cshtml files. So I fixed this for now by just splitting up my Content-Page file in to two parts where in one I do my actions and the other where I display whatever I needed from the actual Content-Page properties. Is that the way to do it or is there still a better solution? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
What you did is the solution: the |
Beta Was this translation helpful? Give feedback.
What you did is the solution: the
Content
shape template gets its model from OC, so you can't use your custom viewmodel there, only in templates that you completely control.