views:

5222

answers:

11

What is prefered way of setting html title (in head) for view when using master pages?

One way is by using Page.Title in .aspx file, but that requires in master page which can mess with HTML code. So, lets assume no server side controls, only pure html. Any better ideas?

UPDATE: I would like to set title in view NOT in the controller or model.

A: 

There is a Title property of the @Page directive for content pages.

sliderhouserules
Yes, but I said above, it requires <head runat=server> in master page, which is not what I want.
bh213
Your question is nonsensical then, because you're saying you want to set the title of your content page using server-side constructs, but you don't want to use server-side constructs.
sliderhouserules
Well, I would like to avoid any aspx controls. <head runat=server> eats some of the html we use, which we could solve, but I would prefer to use only pure html. Master pages and controls should be acceptable though.
bh213
If you're using MVC... you really shouldn't be relying on "runat=server" stuff anyway. Otherwise, why are you using MVC :)
Timothy Khouri
Because MVC is a design pattern that has little to do with the whole "purist" write-it-all-in-the-aspx-page argument.
sliderhouserules
BTW... "Yes, but I said above, it requires <head runat=server> in master page, which is not what I want." You did *not* say that in your post, you should re-read what you posted and fix your post if that's what you meant to say. Proof-reading FTW.
sliderhouserules
+2  A: 

When I create a new MVC project it has files in there and uses a master page. Looking at that it seems it passes the title to the ViewData as ViewData["Title"] and in the master page, in the <head> there is a script block that outputs ViewData["Title"].

metanaito
+11  A: 

I see a lot of people that use the <%= ViewData["Title"] %> option.

I suppose you could also insert a ContentPlaceHolder named Title and then just use that on your page, but in all the MVC examples I've seen, they use the first option.

Hugoware
A: 

You could always use javascript in your view page:

<script type="text/javascript>
    document.title = "Hello World";
</script>
Todd Smith
Don't do this since JavaScript is never a 100% given.
Daniel A. White
A: 

We ended up with

<head runat=server visible=false>

in master page.

This way we can read from Page.Title (Page.Title requires head element to exist, otherwise it throws an exception, checked that with reflector). We then use our own head element - MVC way.

bh213
A: 

For ASP.NET content pages just add Title="" in the <%@ %> Placeholder. :)

This is what I said above.
sliderhouserules
+1  A: 

I ended up using a code-behind file to implement Page.Title="..." in the Page_Load() method.

I didn't like doing this, however attempts to implement the change directly in the .aspx page did not work, as it resulted in two <title> tags being present, the one I generated, and the one generated by the Master file the page inherited from.

Ideally, my page code should have overridden the master file's <title> value, but I guess this is just one of those quirks that ASP.Net MVC still has, and one that may already be fixed in a newer version of the ASP.Net MVC Framework (we're still on ASP.Net MVC Beta)

Nissan
This has changed in the RCs.
Daniel A. White
+18  A: 

In our master pages, we created both an "init" ContentPlaceHolder, and a "title" ContentPlaceHolder. If someone wants to programatically set Page.Title, they can set it in CSharp in the init placeholder, or they can override the "title" placeholder using tags.

Master Page

<asp:ContentPlaceHolder id="init" runat="server"></asp:ContentPlaceHolder>
<head runat="server">    
    <asp:ContentPlaceHolder ID="title" runat="server">
        <title><%=this.Page.Title%></title>
    </asp:ContentPlaceHolder>
</head>

View Page Could either override the entire "title" content placeholder:

<asp:Content ID="Content1" ContentPlaceHolderID="title" runat="server">
       <title>Home Page</title>
</asp:Content>

or programatically set the page title.

<asp:Content ID="Content1" ContentPlaceHolderID="init" runat="server">
    <%this.Title = "Home Page";%>
</asp:Content>

Make sure you remove the Title="" from the Page directive at the top, or you won't be able to programatically change Page.Title.

Andrew Csontos
+1  A: 

A good way is to set the title in your action/controller using an attribute.

See my blog post : http://guillaume-roy.blogspot.com/2010/01/easy-way-to-manage-page-title-in-aspnet.html

Guillaume Roy
A: 

I have a base view class that sets the page title from a resource file. Works pretty good for me.

Esteban Araya
A: 

only in master page

<head runat="server">
   <title><%= (this.Page.Title == "") ? "site name" : this.Page.Title %></title>
</head>
yicone