This project is read-only.

Navigation

So your web-site needs some navigation, that could be static navigation (some pre-defined footer etc.) or a dynamic navigation (user generated etc.).
We have just the right tools for that in the MVC Bootstrap project. Again you'll need to register the navigation with the dependency injection framework. This might seem like a lot of work just to get a simple navigation working, but you'll see why in a bit. First, let's create a static navigation, it's really easy to do a dynamic one, it's just a matter of how you get the list of pages.

using MVCBootstrap.Web.Mvc.Navigations;

public class TopNavigation : NavigationBase {
	private static List<NavigationItem> items = new List<NavigationItem> {
			new NavigationItem { Action = "Index", Controller = "Home", Title = "Home", Visibility = PageVisibility.Always },
			new NavigationItem { Action = "About", Controller = "Home", Title = "About", Visibility = PageVisibility.Always },
			new NavigationItem { Action = "ChangePassword", Controller = "Account", Title = "Change Password", Visibility = PageVisibility.Authenticated }
		};

	public override String Name { get { return "TopNavigation"; } }

	protected override IEnumerable<NavigationItem> Items {
		get {
			return items;
		}
	}
}

public class SiteConfiguration : IDependencyBuilder {
	public void Configure(IDependencyContainer container) {
		container.RegisterPerRequest<INavigation, TopNavigation>();
	}
	public void ValidateRequirements(IList<ApplicationRequirement> feedback) { }
}

Again, remember to add this new builder to the list of builders in the global.asax.cs file.
So now everything is set to work, what then? Put this in you main layout view, or your partial navigation view or where ever you need the navigation:

                <ul id="menu">
@foreach (var page in Html.Navigation(Url, "TopNavigation")) {
					<li>@Html.Link(page.Title, page.URL, page.AdditionalData)</li>
}                </ul>

Easy right? It will even tell you which page is the active one (if any).

Last edited Jul 17, 2012 at 6:18 AM by steentottrup, version 2

Comments

No comments yet.