Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Sam 79 posts 425 karma points
    Nov 17, 2015 @ 14:59
    Sam
    0

    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

  • Tim Miller 32 posts 252 karma points
    Nov 17, 2015 @ 18:53
    Tim Miller
    100

    Sure, I do this kind of stuff fairly regularly.

    @{
        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").

    <div class="@(CurrentPage.DocumentTypeAlias != "Home" ? "inside" : "")">
    
  • Sam 79 posts 425 karma points
    Nov 18, 2015 @ 16:20
    Sam
    0

    That looks like exactly what I'm looking for. Ill give it a try and let you know how it goes.

  • Sam 79 posts 425 karma points
    Nov 18, 2015 @ 17:51
    Sam
    0

    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.

    @{
                        switch((string)CurrentPage.Name)
                        {
                            case "Videos":
                                <div class="">
                                    @foreach(var videoItem in Model.Content.Children(x => x.IsVisible())){
    
                                            @Html.Partial("EduVideos", videoItem)
    
                                    }   
                                </div>
    
                                break;
                            case "Why Natural Stone":
                                <div class="WNS">
                                    @foreach(var WNSItem in Model.Content.Children(x => x.IsVisible())){
    
                                            @Html.Partial("Why Natural Stone", WNSItem)
                                    }
    
                                </div>
    
                                break;
                            default:
    
                                break;
                        }
                    }
    

    Here is the Partial:

    <div class="WNSBox clearfix">
    
            <div class="col-sm-6 col-xs-12 WNSImage " style="background-image:url(@Umbraco.Media(CurrentPage.whyStoneImage).url);">
            </div>
            <div class="col-sm-6 col-xs-12 ">
                <div class="WNSText ">
                    <h2>@Umbraco.Field("whyStoneTitle")</h2>
                    <p>@Umbraco.Field("whyStoneDescription")</p>
                </div>
            </div>
    
        </div>
    

    Thanks again

  • Tim Miller 32 posts 252 karma points
    Nov 18, 2015 @ 18:14
    Tim Miller
    0

    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:

    @Umbraco.Media(CurrentPage.GetPropertyValue<int>("whyStoneImage")).url
    

    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.

  • Sam 79 posts 425 karma points
    Nov 18, 2015 @ 21:18
    Sam
    0

    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.

        @foreach(var WNSItem in Model.Content.Children(x => x.IsVisible())){
    
    <div class="WNSBox clearfix">
    
            <div class="col-sm-6 col-xs-12 WNSImage " style="background-image:url(@Umbraco.Media(CurrentPage.GetPropertyValue<int>("whyStoneImage")).url);">
            </div>
            <div class="col-sm-6 col-xs-12 ">
                <div class="WNSText ">
                    <h2>@Umbraco.Field("whyStoneTitle")</h2>
                    <p>@Umbraco.Field("whyStoneDescription")</p>
                </div>
            </div>
    
        </div>
                }
    

    all of the content for the partial view is a child of the content node

    enter image description here

    Thanks Again.

  • Tim Miller 32 posts 252 karma points
    Nov 18, 2015 @ 21:30
    Tim Miller
    0

    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:

    @Umbraco.Media(WNSItem.GetPropertyValue<int>("whyStoneImage")).url
    
    <h2>@WNSItem.GetPropertyValue("whyStoneTitle")<h2>
    <p>@WNSItem.GetPropertyValue("whyStoneDescription")</p>
    
  • Sam 79 posts 425 karma points
    Nov 18, 2015 @ 21:39
    Sam
    0

    Thank you Tim, Sorry for the newbie questions, I will stop bothering you now.

  • Tim Miller 32 posts 252 karma points
    Nov 18, 2015 @ 21:49
    Tim Miller
    0

    Ha, no problem. Glad I could help. :)

Please Sign in or register to post replies

Write your reply to:

Draft