What I am trying to achieve is, I have created a div section within the master template, which has been applied to the Home Page Doc Type as well as other Pages.
However, I only want to add the class of "home" to the div on the Home page Node, not any other of the pages with contain the div from the master template.
If current page is the home node, add class.
Hi,
I'm pretty new to Umbraco and .NET.
What I am trying to achieve is, I have created a div section within the master template, which has been applied to the Home Page Doc Type as well as other Pages.
However, I only want to add the class of "home" to the div on the Home page Node, not any other of the pages with contain the div from the master template.
Any help would be much appreciated. Thanks.
You can pass parameter via ViewBag from view to master
Hi Harry,
Here's a quick example. I've cut the head section out for clarity.
Hopefully it's readable - I create a variable "isHomepage" - checking if the root id node is the same as the current one.
Then there is a ternary if / else block that adds a class to the body tag in razor.
Ternary is just a posh way of saying shortened if {} else {} https://www.tutorialsteacher.com/csharp/csharp-ternary-operator
The null is a way of not having blank space in the class.
Hi Steve,
Thanks for responding. This is the solution I came up with after doing some research.
However, I will test your method because my method returns an empty class on the pages I do not want to apply the class of "home".
String homeClass = string.Empty; if (Model.Id == 1057) { homeClass = "home"; }
Thanks again for your help.
Thanks @Harry
if (Model.Id == 1057) it worked for me
generally is not so good idea to use an id or page name in code, it can be changed
you can use
pass variable via viewbag, from On Homepage.chtml
ViewBag.BodyClass = "homepage";
On _Layout
is working on a reply...