tags:

views:

104

answers:

5

Using ASP.Net MVC on my Site.Master I have:

<head runat="server">
    <title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /> - MySite</title>
    <link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
</head>

then on each view I have something like:

<asp:Content ID="Title" ContentPlaceHolderID="TitleContent" runat="server">
    Home
</asp:Content>

and I was expecting, as a result:

 <title>Home - MySite</title>

but instead I've got:

 <title>Home</title>

Any ideas why?

+1  A: 

You may have a Title attribute in the <%@ Page %> directive.

Jacob
I did have a Title there, but removing it didn't help. What should I do with it?
J. Pablo Fernández
A: 

wired I had the same issue and I end up using this format

   <title><%= Html.Encode(ViewData["Title"])  %> - mysite</title>

in this way the title is controlled through the controller

Rony
Arguably the view should be responsible for determining the title and not the controller.
Joseph Daigle
I wouldn't even say "arguably." Titles are most certainly view logic, and probably 95% of the time, a dynamic title is based on your model.
Stuart Branham
+5  A: 

Use this:

<title>
  <asp:ContentPlaceHolder ID="titleContent" runat="server" />
  <%= "- My Site" %>
</title>

The reason is that everything rendered in the head is rendered as a control.

See this question for some further links, and other ways of solving it.

boymc
Thank you, that was really annoying me
wheelibin
A: 

Have you tried taking the runat="server" bit out of the head tag? I don't have a machine to test on right now, but that looks a bit odd to me.

Stuart Branham
A: 

I use this markup in Site.Master file:

<title>
    <asp:ContentPlaceHolder ID="TitleContent" runat="server" />
    <asp:Literal runat="server" Text=" - MySite" />
</title>

It looks similar to boymc's suggestion.

Alexander Prokofyev