tags:

views:

497

answers:

2

I'm trying to make a meta tag in my view based on data in my model...my view code looks like this:

<meta name="description" content="<%=Html.Encode(Model.MetaDescription) %>" />

But my output looks like this:

meta name="description" content="&lt;%=Html.Encode(Model.MetaDescription) %>" />

What the heck am I doing wrong?

+2  A: 

You are probably adding this meta tag to a <head> tag which is marked at runat="server". Try:

<meta name="description" content='<%=Html.Encode(Model.MetaDescription) %>' />

I don't have a clear idea about what the actual problem is, as I don't have the full source, but to workaround:

<meta name="description" content=<%= "\"" + Html.Encode(Model.MetaDescription) + "\"" %> />
Mehrdad Afshari
Tried that...no change. If I strip the single or double quotes out it will work...so in the end it looks like: content=this is my contentbut that's probably a bad idea.
Webjedi
Is my assumption correct? Are you using <head runat="server">?
Mehrdad Afshari
Yeah...but I'm wondering now if I need it.
Webjedi
Yep, I was gonna suggest dropping head runat=server if you don't have a need.
Mehrdad Afshari
I stripped it out and it worked...kind of wondering why by default a view has runat in the head tag anyway...oh well.
Webjedi
For example, it's required for setting the Title at the <%@ Page %> directive. I don't like it, by the way :)
Mehrdad Afshari
A: 

Mehrdad's second solution works fine and renders a valid XHTML, but causes VS IDE to complain about the invalid syntax (it doesn't "see" the double quotes at design-time and thinks they are missing).

A better (working) syntax would be:

<meta name="description" content=<%= "" + Html.Encode(Model.MetaDescription) %> /> 

It seems like a bug in the MVC rendering engine, and the empty string in the beginning is a workaround...

Ofer Zelig