2013-10-09 19 views
1

我在Umbraco 6.1.6中創建了一個局部視圖,該視圖生成導航菜單。帶定製控制器的Umbraco 6導航菜單用於預處理

@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> 

我想在後臺代碼中獲得菜單項列表,並在呈現視圖中的列表之前對其進行進一步處理。我將如何做到這一點?我應該創建一個自定義控制器還是什麼?我不想在視圖代碼中執行額外的處理。

感謝

回答

0

我挖成MVC和一把umbraco位更多,並創建了一個使用自定義控制器的解決方案。基本的評價是這樣的。

在項目中創建的模型文件夾中的型號

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; } 
    } 
} 

創建視圖一個新的局部視圖>共享文件夾

@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> 

創建SurfaceController執行像獲取節點的一些業務邏輯並建立模型

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); 
     } 
    } 
} 

調用你的新的局部視圖使用這行代碼的任何地方:

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

我還張貼了這個對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

2

我想創建一個擴展方法,並將其放置在AppCode文件夾:

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

,比你的觀點

@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 
} 
+0

這是在.NET MVC做事的首選方式是什麼?我是一名Java開發人員,我在Spring MVC中的做法是對Controller中的數據進行預處理(或讓Controller將處理委託給服務)。 – Julius

+0

如果它是普通的.NET MVC解決方案,它不會是首選的方式,但您可能在控制器中具有該功能,但在我看來,使用umbraco創建控制器僅用於此目的是一種開銷。我現在在umbraco中使用擴展方法已經有好幾年了,它對我來說非常好用(易於訪問,extendibale等)。 –

+0

您的解決方案可以工作,但我更喜歡控制器,因爲我認爲從架構的角度來看它更好。我發佈了下面我製作的解決方案。謝謝一堆。 – Julius