switch statement for loading partial views into a template based on a current page variable.
Hello All,
I am new to both Umbraco and .net, But i am thinking I can make better use of my basic template pages by stacking partial views into a switch statement that would load based on a current page variable.
Currently I am Creating an additional template so i can load different partial views into different pages, and it seems like it is not the most efficient way to do this.
Is it possible to do what I described above, or is there another method that I should know about?
@{
switch((string)CurrentPage.DocumentTypeAlias)
{
case "HomePage":
@Html.Partial("PartialOne")
break;
case "InsidePage":
@Html.Partial("PartialTwo")
break;
default:
@Html.Partial("DefaultPartial")
break;
}
}
Here's something else I do occasionally too. Sometimes I have two basic layouts and I just need to add a class that I can reference in my CSS. Here's an example of adding an "inside" class to a div element on any page that is not the home page (document type alias "Home").
The ambiguous problem looks like it is actually being caused by your background image. CurrentPage.whyStoneImage isn't a thing. Try this for your background image instead:
That should get your code working. If it were me, I think I would probably move the foreach statements into the partial views and just have the call to the partials in the switch statement. That would clean up your layout and put all of the item logic together.
Inside the foreach you are working with the individual child nodes which at that point are assigned to the WNSItem variable and should have a type of IPublishedContent. So here is how you would reference the data:
switch statement for loading partial views into a template based on a current page variable.
Hello All,
I am new to both Umbraco and .net, But i am thinking I can make better use of my basic template pages by stacking partial views into a switch statement that would load based on a current page variable.
Currently I am Creating an additional template so i can load different partial views into different pages, and it seems like it is not the most efficient way to do this.
Is it possible to do what I described above, or is there another method that I should know about?
Thanks in Advance
Sure, I do this kind of stuff fairly regularly.
Here's something else I do occasionally too. Sometimes I have two basic layouts and I just need to add a class that I can reference in my CSS. Here's an example of adding an "inside" class to a div element on any page that is not the home page (document type alias "Home").
That looks like exactly what I'm looking for. Ill give it a try and let you know how it goes.
Tim,
This seems to work, but you might be able to help me refine this.
The partial views I have require a foreach loop, and I am including that in the switch statement. So the code looks a little long.
Is there a better way to write the foreach loop inside the partial view. I get and ambigous error because of the var name.
Here is the Partial:
Thanks again
The ambiguous problem looks like it is actually being caused by your background image. CurrentPage.whyStoneImage isn't a thing. Try this for your background image instead:
That should get your code working. If it were me, I think I would probably move the foreach statements into the partial views and just have the call to the partials in the switch statement. That would clean up your layout and put all of the item logic together.
Thanks Tim, That Solved the error issue, but now I have another.
I moved the foreach inside of the partial, and the html renders correctly, but the data does not render at all.
all of the content for the partial view is a child of the content node
Thanks Again.
Inside the foreach you are working with the individual child nodes which at that point are assigned to the WNSItem variable and should have a type of IPublishedContent. So here is how you would reference the data:
Thank you Tim, Sorry for the newbie questions, I will stop bothering you now.
Ha, no problem. Glad I could help. :)
is working on a reply...