tags:

views:

29

answers:

2

Hi

I always heard the benefit of making html helpers but no one really shows how it would look if you did not right an html helper.

So I just wanted to write a label with plan html and not my html helper I made

<label for="<% ViewData["View_CoursePrefix"].ToString(); %>"></label>

I am trying to put a viewData in the "for" part but it keeps saying semi colon expected. I tried to escape the quotes but that does not work. What do I need to do to make this work?

A: 
<label for="<%=ViewData["View_CoursePrefix"] %>">some text</label>
hunter
why can't you do toString()? I know the equal complies it to the string but I am just wondering the other way too.
chobo2
+3  A: 

It's the difference between <% %> & <%= %>

When you write:

<%= ViewData["View_CoursePrefix"] %>

Behind the scenes, the WebForms view engine translates it to:

<% Response.Write(ViewData["View_CoursePrefix"]); %>

So to get your version to work, you'd have to wrap it in Response.Write().

HTHs,
Charles

Oh, and FYI... In ASP.NET 4.0 they're introducing <%: %> which will automatically HtmlEncode the output. See this blog post from Phil Haack

Charlino