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?
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.
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:
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.
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.
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?
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.
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.
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.
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.
4. Call the view from anywhere
Now you can call the navigation menu from anywhere with this line of code:
Someone might want to rename the title to
Navigation menu with PartialView and custom SurfaceController in Umbraco 6
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
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.
@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.
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
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
It might also help to have a look at the Hybrid Framework. It shows how MVC can be used in v6.
Jeroen
is working on a reply...