1

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.

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I can't imagine using this technique for a huge site.
Did you down voted this answer because you "can't imagine using this in a huge site?"
No. It's just that it doesn't seem maintainable.
0

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

Comments

0

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.

Comments

-1

You can use sitemap and a few tricks for that.

http://www.asp.net/(S(pdfrohu0ajmwt445fanvj2r3))/learn/mvc/tutorial-20-cs.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.