I'm having a hard time understanding the best way to code something clean with Umbraco. By that, I mean that a lot of code is in my templates which is not the cleanest work ever...
For example, my file "department.cshtml" starts with :
All this code would normally go to the file code-behind "department.cs" attached to my Razor page. But, if I try to use this way, I can't use some code for Umbraco like :
You should also maybe look into using a rendermvccontroller to hijack the default renderring, you could then put your logic into the controller and pass a custom viewmodel to the view
When you create a new Document Type you have the option of creating a Template along with it. You would normally choose the template option if your Document Type represents a page. This will create a View in your ~/Views folder.
Partial Views can then be used to reduce the code in your main Template. Partial Views are often used to hold code that may be repeated across your main Templates. This way you dont need to replicate code over and over again if it is common to different pages of your site.
For example a common use case is to put the logic behind your site navigation within a partial view. You would then add this partial view to each page template (View) so that you are not replicating this code. You can read more about partial views here:
How to code with Umbraco and Razor pages ?
Hi all,
I'm having a hard time understanding the best way to code something clean with Umbraco. By that, I mean that a lot of code is in my templates which is not the cleanest work ever...
For example, my file "department.cshtml" starts with :
@using Umbraco.Cms.Web.Common.PublishedModels; @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage @{ Layout = "master.cshtml"; ViewBag.Title = Model.Value("title");
All this code would normally go to the file code-behind "department.cs" attached to my Razor page. But, if I try to use this way, I can't use some code for Umbraco like :
How can I do this ?
Thank you all in advance for your time and patience.
Disclaimer : I'm a baby dev currently doing her first project ever, sorry if all of this sounds stupid, I'm learning. ☺
Umbraco uses the MVC pattern so there are no codebehind files in it's view templates, code is placed in the view itself.
Hi,
Thanks for the reply ! So is it usual to have like 50 lines of code in my template ? I am a bit confused because it looks like a mess to me 😁
However you can take your code away from the partial views using View Components.
This is a technique I would use for more complicated logic.
https://docs.umbraco.com/umbraco-cms/v/13.latest-lts/reference/templating/mvc/viewcomponents
Hi Marc !
Thanks for the reply and the documentation, it all still seems confusing to me but I will definitely look more into it.
You said "for the partial views" but does it work for the "normal" Views ?
I don't really know how to clean my Views... I have sometimes around 100 lines of code in my template...
Thanks again. ☺
You should also maybe look into using a rendermvccontroller to hijack the default renderring, you could then put your logic into the controller and pass a custom viewmodel to the view
https://docs.umbraco.com/umbraco-cms/v/13.latest-lts/reference/routing/custom-controllers
Hi Jessica,
When you create a new Document Type you have the option of creating a Template along with it. You would normally choose the template option if your Document Type represents a page. This will create a View in your ~/Views folder.
Partial Views can then be used to reduce the code in your main Template. Partial Views are often used to hold code that may be repeated across your main Templates. This way you dont need to replicate code over and over again if it is common to different pages of your site.
For example a common use case is to put the logic behind your site navigation within a partial view. You would then add this partial view to each page template (View) so that you are not replicating this code. You can read more about partial views here:
https://docs.umbraco.com/umbraco-cms/v/13.latest-lts/reference/templating/mvc/partial-views
Cheers,
Marc
is working on a reply...