Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Element 'title' occurs too few times, XHTML validation warning in ASP.NET.MVC master page

Tags:

asp.net-mvc

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.

like image 474
Blegger Avatar asked Feb 27 '09 17:02

Blegger


2 Answers

Here possible solutions are First solution is

<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> - this line should be removed.
</asp:ContentPlaceHolder>

second solution is, Check whether the head tag having attribute runat="server",if have not set runat prperty means not a problem else need to remove the runat tag.

like image 45
Raja Avatar answered Sep 21 '22 12:09

Raja


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.

like image 155
John Sheehan Avatar answered Sep 21 '22 12:09

John Sheehan