To answer your questions:
There are many ways to create navigation links for you ASP.NET MVC, whatever works for you is the best.
Most would answer that class files should be placed in Model folder. I found it more meaningful to place ViewModel classes in a separate folder and classes used throughout the application (business logic/meat of the application) in a separate file.
What you're trying to accomplish seems like a job for extension methods. Here is a good tutorial: http://www.dotnetcurry.com/ShowArticle.aspx?ID=406&AspxAutoDetectCookieSupport=1
What you're doing is on the right track, however, you need to create static classes and static functions/methods for this to work properly.
http://msdn.microsoft.com/en-us/library/bb383977.aspx has some general info on extension methods.
One quick note: To allow usage of all the extension methods you create, you'll need to reference the class/namespace that you placed them in.
There are two methods of doing this:
Assuming you've placed your extension methods in MvcApplication1.MyExtensionMethods, Add the following after the
<page>
<namespaces>
tag in your application's web.config (Not the View's web.config file)
<add namespace="MvcApplication1.MyExtensionMethods"/>
Doing so will allow the extension methods to be used in all of your views (.aspx/.ascx) files.
Place
<%@ Import Namespace="MvcApplication1.MyExtensionMethods" %>
at the top of your .aspx/.ascx files. You would need to do this for every file you need to use the extension methods on (not efficient)
The following is what I implement and it has served me well thus far.
NavigationLink.cs
public class NavigationLink
{
string Text {get; set;}
RouteValueDictionary Routes {get; set;}
string RouteName {get; set;}
}
NavigationLink.ascx (Place in shared folder for easy access in entire application)
(Note: I wrap the links in < li> < /li> tags because I use lists for all my navigation controls. You can then place these links in any type of list, allowing freedom with the list's class/style.)
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<NavigationLink>>">
<% foreach(NavigationLink n in Model){ %>
<li>
<a href="<%= string.IsNullOrEmpty(n.RouteName) ? Url.RouteUrl(Routes) : Url.RouteUrl(RouteName) %>">
<%= n.Text %>
</a>
</li>
<% } %>
Menus.cs (Some examples)
public static Menus
{
public static List<NavigationLink> MainMenu()
{
List<NavigationLink> links = new List<NavigationLink>();
links.Add(new NavigationLink{
Text = "Home",
Routes = new RouteValueDictionary(new { action="Index", controller="Home" })
});
return links;
}
public static List<NavigationLink> UserMenu()
{
List<NavigationLink> links = new List<NavigationLink>();
links.Add(new NavigationLink{
Text = "Messages",
Routes = new RouteValueDictionary(new { action="Messages", controller="UserCP" })
});
links.Add(new NavigationLink{
Text = "Account",
Routes = new RouteValueDictionary(new { action="Account", controller="UserCP" })
});
return links;
}
}
Now that you have everything setup, calling these functions is simple:
In your view file (.aspx/.ascx)
<ul class="horizontal_menu">
<% Html.RenderPartial("NavigationLink", MyMvcApplication1.Menus.MainMenu()) %>
</ul>
Having it setup like this allows for different partial views to be created to render out lists of navigation links in different ways and would require that you only build the partial and call it.