tags:

views:

2055

answers:

1

I am getting the following XHTML validation warning in my ASP.NET MVC master page:

Validation (XHTML 1.0 Transitional): Element 'title' occurs too few times.

The title tag for the master page is included in the ContentPlaceHolder in the head tag as shown in the code below. The title tag in the ContentPlaceHolder is not taken into account when performing the validation, and I do not want to just add another one in the head tag because then I will be left with two title tags.

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta http-equiv="Content-Style-Type" content="text/css" />
    <asp:ContentPlaceHolder ID="head" runat="server">
        <title></title>
    </asp:ContentPlaceHolder>
</head>

One work around that I have found is to use the following technique in the head tag:

<% if (false) { %>
    <title></title>
<% } %>

Is this the best practice to resolve this warning? I am not a huge fan of adding the excess code just to pass validation warnings but I will live with it if there is not a better alternative.

+9  A: 

Do this instead:

<head>
    <title><asp:ContentPlaceHolder ID="title" runat="server">Default Page Title Here</asp:ContentPlaceHolder></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta http-equiv="Content-Style-Type" content="text/css" />
    <asp:ContentPlaceHolder ID="head" runat="server"></asp:ContentPlaceHolder>
</head>

Or as an alternate, set the title programattically from each page.

What's happening in your case is that when a new view is created, it creates empty content items which override the default content in the placeholders. If you remove the empty content blocks from the view, the default placeholder content will be used, but then you can't set the contents from the view. Using the code above you can override a default title from each view and include scripts, etc. in the head independently of each other.

John Sheehan
I am surprised that is not the default syntax they generate when you create a new ASP.NET-MVC project. Thanks for the quick response!
Blegger
They don't generate it like that by default because the Title can be set in the view Page directive or a plethora of other ways.
John Sheehan
I've seen contentplaceholders for the head, but never one specifically for the title tag. Although it sounds stupid as all get out, its beautiful, lol. +1
TheTXI
And here I was agonizing over where that silly extra blank title element in my head came from ...
qstarin