views:

88

answers:

3

I have a controller's action and view page that uses a master page.

The master page has the html title section like:

<title>this is the page's title</html>

How can I access this section from within my controller's action (preferably) or my action's view page?

+2  A: 
<title><%= Model.PageTitle %></html>

public class MasterModel
{
    public string PageTitle { set; get; }
}

public class MyViewModel : MasterModel
{
    /* ... */
}

You can set the base class PageTitle property in a controller action all you want.

public ActionResult SomeAction ()
{
    MyViewModel model = new MyViewModel ();
    model.PageTitle = "this is the page's title";

    return View (model);
}
Developer Art
How do I get the master page to use the MasterModel though? You are refering Model in the master page?
Blankman
Ok I just referenced the base model in the master, thanks ALLOT!
Blankman
Yep, the master page would be strongly-typed to the base class (MasterModel), while the views would use the derived model classes.
Developer Art
A: 

Pop the title you want into ViewData from the Action method and then just render it to the page...

In action method

ViewData["PageTitle"] = "Page title for current action";

On master page

<!-- MVC 1.0 -->
<title><%=Html.Encode(ViewData["PageTitle"]) %></title>

<!-- MVC 2.0 -->
<title><%: ViewData["PageTitle"] %></title>
Simon Fox
yeah the problem is I use strongly typed ViewData for each action.
Blankman
+1  A: 

The way I typically handle setting the title of html pages is through the pages Title property.

In my view I have this...

<%@ Page Language="C#" Title="Hello World" ... %>

And in my master page I have this...

<title><%=Page.Title%></title>

If you want to have the controller set the page title you will probably have to send the title in through the view model.

Brian
but on dynamic pages, how can you just hard code 'hello world' into the title?
Blankman