Copied to clipboard

Flag this post as spam?

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


  • Peter Rosendahl 3 posts 73 karma points
    May 08, 2018 @ 08:04
    Peter Rosendahl
    0

    HowTo: Surface Controller return multiple partial views by foreach/switch loop

    Is there any way from a surface controller to render partial views related to published child document types?

    What I want to do is to keep my views as code-minimized as possible, leaving most of the code in my surface controllers. When it comes to generating various partial views relevant to the published child document types, it seems that the only possible way to do so is by using a foreach loop inside a partial view. But is there a way that I can move this loop to a surface controller, so that the partial view remains simplified?


    This is what I've considered doing:

    • In my View, I activate a Html.RenderPartial
    • In my Partial View, I call the Action in my Surface Controller, where I want the loop to iterate
    • In my Surface Controller Action, I want to generate a foreach loop for every published child document type
    • In my loop, I want to use a switch based on DocumentTypeAlias of the child element
    • In my switch cases, I want to activate an ActionResult method for the specific case, wherein I return a PartialView for that child element.

    I've tried this way of solving the task, but the loop cannot show the partial views, that it's supposed to render by each published child element.

    Does the activated string Method (called from the partial view) have to be of ActionResult type for the nested ActionResult methods to work properly?

    And can an ActionResult method return multiple child ActionResult values?

    Or is it not possible to generate this kind of loop with multiple partial views inside one Controller?


    Here is an example of the code from the SurfaceController:

    public string GetFrontPageElements(PageViewModel model)
        {
            StringBuilder _sb = new StringBuilder();
            UmbracoHelper umbHelper = new UmbracoHelper(UmbracoContext.Current);
            bool existsButUnpublished = false;
    
            Node frontpageNode = GetNodeByAlias(model.ParentAlias, out existsButUnpublished);
            if (frontpageNode != null)
            {
                _sb.AppendLine("<link href='~/css/customstyle.css' rel='stylesheet' />");
                IPublishedContent frontpageContent = Umbraco.TypedContent(frontpageNode.Id);
                foreach (var partialElement in frontpageContent.Children)
                {
                    switch (partialElement.DocumentTypeAlias)
                    {
                        case (firstPartialView):
                            CreateFirstPartialView(frontpageContent);
                            break;
    
                        case (secondPartialView):
                            CreateSecondPartialView(frontpageContent);
                            break;
    
                        default:
                            break;
                    }
                }
            }
    
            return _sb.ToString();
        }
    
        private ActionResult CreateFirstPartialView(IPublishedContent parentContent)
        {
            return PartialView("FirstPartial", parentContent);
        }
    
        private ActionResult CreateSecondPartialView(IPublishedContent parentContent)
        {
            return PartialView("SecondPartial", parentContent);
        }
        // more similar methods to come for the rest of the switch cases
    
  • pranjal 75 posts 188 karma points
    May 08, 2018 @ 10:27
    pranjal
    0

    I am not getting your question. please elaborate what you have to do?

  • Peter Rosendahl 3 posts 73 karma points
    May 08, 2018 @ 11:07
    Peter Rosendahl
    0

    In Umbraco Control panel, I have the following document type setup:

    • FrontPage (parent document type)
      • Slideshow element (child doc. type)
      • Text box element with welcome text (child doc. type)
      • Google Maps iFrame element (child doc. type)
      • Image Gallery Element (child doc. type)

    Each of the child elements have their own Partial View to display their respective content.

    At first, I have just made a View that had a Html.RenderPartial command for each of the child elements. But if my customer wants to change the sort order of the elements shown in the page by the way they are sorted in Umbraco Control Panel, I have an idea of making a foreach loop (that iterates through the published child elements) with a switch, where each case handles the relevant Html.RenderPartial() according to the document types that the loop iterates through.

    To my current understanding, this loop and switch has to be made in the View, since I cannot find any solution online, where this has been done in a SurfaceController and worked in relation to dealing with multiple Partial Views in one SurfaceController.

    But, I want to see if there is any way that I can make this loop work in a SurfaceController instead of a View. I want to see if this is possible, because I want to keep frontend markup and programming code separated from each other as much as possible.

    So, my question is: Can this sort of iteration of Partial Views in a loop function only happen in a View/PartialView, or is it possible to make this loop work in one single SurfaceController, that handles multiple PartialViews accordingly?

Please Sign in or register to post replies

Write your reply to:

Draft