Julius
Julius

Reputation: 2864

Umbraco 6 navigation menu with custom controller for preprocessing

I created a partial view in Umbraco 6.1.6 that generates a navigation menu.

@inherits UmbracoTemplatePage
@using System.Collections;
@using System.Linq;
@{
   Layout = null;
   var articleParent = Model.Content.AncestorOrSelf(1);
}


<ul>
    @foreach (var page in articleParent.Descendants("Artikel").Where(x => x.IsVisible()))
{
   <li><a href="@page.NiceUrl()">@page.Name</a></li>
}

</ul>

I want to get this list of menu items in backend code and do further processing on it before rendering the list in the view. How would I do this? Should I create a custom controller or something? I don't want to do th eextra processing in the view code.

Thanks

Upvotes: 1

Views: 1777

Answers (2)

Julius
Julius

Reputation: 2864

I dug into MVC and Umbraco a bit more and created a solution that does use a custom controller. The basic appraoch is this.

Create a Model in the Models folder of your project

namespace MyProject.Models
{
    public class MenuModel
    {
        // My Model contains just a set of IPublishedContent items, but it can
        // contain anything you like

        public IEnumerable<IPublishedContent> Items { get; set; }
    }
}

Create a new partial view in the Views > Shared folder

@inherits UmbracoViewPage
@{
   Layout = null;
}
<ul> 
    @* Iterate over the items and print a link for each one *@
    @foreach (var page in Model.Items)
    {
        <li><a href="@page.Url()">@page.Name</a></li>
    }
</ul>

Create a SurfaceController to perform some business logic like fetching nodes and building the Model

using System.Web.Mvc;
using MyProject.Models;
using Umbraco.Core;
using Umbraco.Web;
using Umbraco.Web.Models;
using Umbraco.Web.Mvc;

namespace MyProject.Controllers
{
    public class NavMenuController : SurfaceController
    {
        public ActionResult Render(RenderModel some)
        {
           // Get the current homepage we're under (my site has multiple, because it is multi-language)
           var currentHomePage = CurrentPage.AncestorOrSelf(1);
           // Create model object
           var menuModel = new MenuModel();
           // Select descendant "Artikel" nodes of the current homepage and set them on the menu model
           menuModel.Items = currentHomePage.Descendants("Artikel").Where(x => x.IsVisible());
           // Return the partial view called NavMenu

           // Do any processing you like here...

           return PartialView("NavMenu", menuModel);
         }
    }
}

Call your new partial view from anywhere using this line of code:

@Html.Action("Render", "NavMenu")

I also posted this on our.umbraco.org:

http://our.umbraco.org/forum/developers/api-questions/45339-Umraco-6-Looking-for-the-MVC-equivalent-of-codebehind-file?p=0#comment163126

Upvotes: 0

Davor Zlotrg
Davor Zlotrg

Reputation: 6050

I would create an extension method and place it in the AppCode folder:

public static NodesExtensions
{
    public static void Process(this DynamicNodeList nodes)
    {
        foreach(var node in nodes)
        {
            //process node
        }
    }
}

And than in your view

@inherits UmbracoTemplatePage
@using System.Collections;
@using System.Linq;
@{
   Layout = null;
   var articles = Model.Content
                       .AncestorOrSelf(1)
                       .Descendants("Artikel");
   articles.Process();

   //you can now render the nodes 
}

Upvotes: 2

Related Questions