views:

30

answers:

2

I have this simple snippet with an ActionLink that is supposed to display some text as a link, but it's not working.

Here's the code snippet.

<div id = "Div1">
        <table id = "Table1">
            <% while ((category = SomeNamespace.Helper.GetNextCategory(categoryIndex++)) != null)
               { %>
                <tr>
                    <td class = "catalogCell">
                        <% Html.ActionLink(category.Name, 
                               "DisplayCategory", 
                               "Catalog"); %>
                    </td>
                </tr>
            <% } %>
        </table>
    </div>
+1  A: 

You need an = sign:

<%= Html.ActionLink(category.Name, 
                    "DisplayCategory", 
                    "Catalog") %>
RedFilter
I tried that, too. But when I do that, it gives me an exception saying, ") expected".
Water Cooler v2
Holy cow, I did that again, and removed the semi-colon at the end of the method call and it worked. Strange!
Water Cooler v2
Right, I removed that from my example.
RedFilter
But why is that we have to remove the semi-colons sometimes and sometimes not?
Water Cooler v2
Because `<%=` is equivalent to `Response.Write`, which takes a string as its parameter, not a `C#` statement.
RedFilter
+1  A: 

Use the <%: ... %> style and be sure to remove the semi-colon (;) at the end of the statement.

Jonathan Bates
Yes, I did that. But why is that?
Water Cooler v2
Which part? The `<% ... %>` is short hand for `<%= Server.HtmlEncode(...) %>`, or in other words, from the inside out, HtmlEncode this data, then write it to the output. For that matter, that is just short hand for this: `<% Response.Write(Server.HtmlEncode(...)); %>`You don't need the semi-colon (;) at the end, because it is understood that if you are doing `<%= ... %>` or `<%: ... %>` that you are actually doing a `Response.Write(...);` and therefor you get the semi-colon for free. what a bargain! Over your life time, those key stroke savings add up.
Jonathan Bates