Reputation: 115
So here's the deal
I'm working on a project that I had originally focused on Zend Framework, But now I want to get into ASP.NET MVC and boy lets just say it's good and a great start though i'm still at the beginning stage of it. But I am having issues with passing Data From My Controller to the Master / Layout Page. Now in Zend I am able to determine which controller and which action I am in through a helper I created. Now I want to implement the same functionality in this ASP.NET MVC application. So my Master Layout Page knows which Controller I am in and hence highilighting the navigation for it. I am using a straight custom html li a structure navigation.
Any help on this specific topic would be greatly appreciated.
Upvotes: 1
Views: 185
Reputation: 1928
If you want to pass data to your masterpage, I would suggest the following:
Create viewmodel base class, e.g.
public class ViewModelBase
{
public List<MenuItem> MenuItems { get; set; }
public string SomeRandomData { get; set; }
}
modify masterpage's 1st row
<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage<ViewModelBase>" %>
For every page create viewmodels, e.g.
public class TestViewModel : ViewModelBase
{
public string Message { get; set; }
}
Controller example
public class TestController : Controller
{
public ActionResult Index()
{
TestViewModel model = new TestViewModel();
// passing selected menuitem as parameter
model.MenuItems = createMenuItems("test");
model.Message = "Hello World!";
return View(model);
}
private List<MenuItem> createMenuItems(string selected)
{
// ...
}
}
Viewpage's 1st row
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<TestViewModel>" %>
Now you can access ViewModelBase from masterpage and TestViewModel from viewpage :)
Add
<add namespace="yourproject.Models"/>
to 'namespaces' section in web.config so you don't have to type import statement on every viewpage.
Upvotes: 0
Reputation: 1847
Welcome to asp.net mvc... I handle this scenario 1 of 2 ways:
1) I put a line of code at the top of my Master Page that gets the current action being called:
<% string action = ViewContext.RouteData.Values["action"].ToString(); %>
Then, you can do a check on your navigation links and add a class if appropriate:
<% if (action == "Home") { Response.Write(" class='current'"); }%>
2) I send a string along with each view and attach it to the body tag as a class in my Master Page:
public ActionResult Home()
{
ViewData["BodyClass"] = "home";
return View();
}
<body class="<%= (string)ViewData["BodyClass"] %>">
Then in your css you can do something like:
.home #nav li a {
/* something different */
}
Hope that helps.
Upvotes: 2
Reputation: 6302
You can use sitemap and a few tricks for that.
http://www.asp.net/(S(pdfrohu0ajmwt445fanvj2r3))/learn/mvc/tutorial-20-cs.aspx
Upvotes: -1
Reputation: 8959
You might want to take a look at some of the videos published on the ASP.Net site. There are tutorials and screencasts demonstrating the techniques you're asking about. http://www.asp.net/mvc
Upvotes: 0