Hey guys,
I am trying to pass data in my Master page by implementing classes as below:
File: ~/Controllers/ATController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using Gateway.Library;
namespace Gateway.Controllers
{
public abstract class ATController : Controller
{
protected override void Initialize(System.Web.Routing.RequestContext requestContext) {
base.Initialize(requestContext);
ATMenu menu = new ATMenu();
menu.Add("Home", "Home", "Index");
menu.Add("The Market", "Market", "Index");
menu.Add("News & Talk", "News", "Index");
menu.Add("The Committee", "Committee", "Index");
menu.Add("Research & Tools", "Research", "Index");
menu.Add("My Portfolio", "Users", "Portfolio", new List<ATPage>() {
new ATPage("Users", "Register"),
new ATPage("Users", "Login")
});
ViewData["menu"] = menu;
}
}
}
File: ~/Library/ATMenu.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Gateway.Library {
public class ATMenu {
private List<ATMenuItem> _items = new List();
public List items {
get { return _items; }
}
public void Add(string title, string controller, string action) {
this._items.Add(new ATMenuItem(title, controller, action));
}
public void Add(string title, string controller, string action, List pages) {
this._items.Add(new ATMenuItem(title, controller, action, pages));
}
}
}
File: ~/Library/ATMenuItem.cs
using System;
using System.Collections.Generic;
namespace Gateway.Library {
public class ATMenuItem {
private string _title;
public string title {
get { return _title; }
}
private string _controller;
public string controller {
get { return _controller; }
}
private string _action;
public string action {
get { return _action; }
}
private List<ATPage> ActiveIn = new List();
public ATMenuItem(string title, string controller, string action) {
this.__MenuItem(title, controller, action);
this.Activate(new ATPage(this.controller, this.action));
}
public ATMenuItem(string title, string controller, string action, List pages) {
this.__MenuItem(title, controller, action);
this.Activate(new ATPage(this.controller, this.action));
if (pages.Count > 0) {
foreach (ATPage p in pages) {
this.Activate(p);
}
}
}
private void __MenuItem(string title, string controller, string action) {
this._title = title;
this._controller = controller;
this._action = action;
}
public void Activate(ATPage p) {
if (!this.IsActiveIn(p)) {
this.ActiveIn.Add(p);
}
}
public bool IsActiveIn(ATPage p) {
return this.ActiveIn.Contains(p);
}
}
}
File: ~/Library/ATPage.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Gateway.Library {
public class ATPage {
private string _controller;
public string controller { get { return _controller; } }
private string _action;
public string action { get { return _action; } }
public ATPage(string controller, string action) {
this._controller = controller;
this._action = action;
}
}
}
File: ~/Views/Shared/Site.Master
<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
<%@ Import Namespace="Gateway.Library" %>
<ul>
<%foreach (ATMenu menu in (IEnumerable)ViewData["menu"]) { %>
<%foreach (ATMenuItem m in menu.items) { %>
<%=m.title %>
<%} %>
<%} %>
It compiles fine, but whenever I run the application I get the following error:
Unable to cast object of type 'Gateway.Library.ATMenu' to type 'System.Collections.Generic.IEnumerable`1[Gateway.Library.ATMenu]'.
I am sure I must be ignoring something really simple and basic. But not able to figure out as of now.
Would you be please so kind to help me out here?
Thank you