Copied to clipboard

Flag this post as spam?

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


  • julius 107 posts 289 karma points
    Oct 09, 2013 @ 19:48
    julius
    0

    Umraco 6: Looking for the MVC equivalent of codebehind file

    Hello everyone,

    I am looking for the equivalent of codebehind files in Umbraco 6. Here is the case: I created a simple Partial View that generates a navigation menu.

    @inherits UmbracoTemplatePage
    @using System.Collections;
    @{
        Layout = null;
        var articleParent = Model.Content.AncestorOrSelf(1);
    }
    @foreach (var page in articleParent.Descendants("Artikel").Where(x => x.IsVisible()))
    {  
      <li><a href="@page.NiceUrl()">@page.Name</a></li>
    }

    Now I want to do some preprocessing on the descendants of articleParent before rendering them. The kind of processing is not important. Let's say I want to call a webservice and post the national anthem to Facebook for each descendant. Stuff you don't want to do in view code. 

    How and where would I do this? In "old" masterpages Umbraco I would use a codebehind file for this and feed the result of the processing to the ascx view file. 

    Could you point me to an equivalent (hardcore processing --> result --> 'thin' view) solution in Umbraco 6 with MVC? Do I need a custom controller?

  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Oct 10, 2013 @ 15:04
    David Brendel
    0

    I would suggest you to use a custom controller if you want to do it with MVC.

    So you can call the controller action, do the prepocessing and return a partial view where you can output everything like you're doing at in your post.

    Or maybe return a custom model with your pages you want in your navigation.

  • julius 107 posts 289 karma points
    Oct 10, 2013 @ 17:11
    julius
    100

    Ok, in the meantime I indeed worked out a solution for a navigation menu with a partial view and a custom controller. Here it is. I am going to document it like a good boy so other Umbraco 6 / MVC n00bies may benefit.

    1. Create the Model

    Since I wanted to use a strongly typed view I created a Model object called MenuModel. Put the MenuModel class in the Models folder of your MVC project.

    using System.Collections.Generic;
    using Umbraco.Core.Models;

    namespace PatentVista.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; }
       }
    }

     

    2. Create the Partial View

    The partial view is called NavMenu.cshtml and receives a MenuModel object. I put NavMenu.cshtml in the Views > Shared folder of my project.

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


    3. Create a SurfaceController

    Now create a custom Controller in the Controllers folder. Mine is called NavMenuController. Not sure if the controller and view name have to match (the beginning at least), but I chose to match them.

    using System.Web.Mvc;
    using PatentVista.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
               return PartialView("NavMenu", menuModel);
             }
        }
    }

     

    4. Call the view from anywhere

    Now you can call the navigation menu from anywhere with this line of code:


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

     

  • julius 107 posts 289 karma points
    Oct 10, 2013 @ 17:33
    julius
    0

    Someone might want to rename the title to 

     

    Navigation menu with PartialView and custom SurfaceController in Umbraco 6

  • Martin Griffiths 826 posts 1269 karma points c-trib
    Oct 10, 2013 @ 18:28
    Martin Griffiths
    0

    I've done similar to you recently to build a list of our YouTube videos from our feed using the YouTube .net API.

    One thing worth changing in your partial view is 

    @inherits UmbracoViewPage<YourModel>
    

    This will give you access to the @Umbraco Helpers which can be a slightly quicker route to things rather than from IPublishedContent

    It's a nice helping hand to suff! I do both and choose the best method as required.

    M.

  • Martin Griffiths 826 posts 1269 karma points c-trib
    Oct 10, 2013 @ 18:34
    Martin Griffiths
    0

    @Julius I just noticed in Umbraco 4.11.x it's the ONLY way you can do it. I guess it's changed in v6, so it may not be relevent/possible.

    M.

  • julius 107 posts 289 karma points
    Oct 12, 2013 @ 13:13
    julius
    0

    Hi Martin, 

    Thanks. I actually just bumped into the problem of having no access to the Umbraco helper in a contact form I am now building and your solution does work.

    This gives access to the ContactFormModel, but not to the Umbraco helper

    @model MyProject.Models.ContactFormModel
    And this will give access to both the Model object and the helper.
    @inherits UmbracoViewPage<MyProject.Models.ContactFormModel>
  • Martin Griffiths 826 posts 1269 karma points c-trib
    Oct 14, 2013 @ 11:02
    Martin Griffiths
    0

    Hi Julius

    Glad it was useful!

    I had some help from Sebastiaan Janssen on a thread I ended up hijacking! lol. You can see from this what I was up to!

    http://our.umbraco.org/forum/developers/extending-umbraco/45316-Working-with-a-mature-WebForms

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Oct 14, 2013 @ 12:11
    Jeroen Breuer
    0

    It might also help to have a look at the Hybrid Framework. It shows how MVC can be used in v6.

    Jeroen

Please Sign in or register to post replies

Write your reply to:

Draft