0

This is a very newbie kind of ASP.NET question: I simply don't know and can't work out the correct syntax to use.

In my view I want to generate an action link if a certain condition is true on my model. I know how to generate a link using this syntax:

<%: Html.ActionLink("Do Something", "DoSomething", new { id = Model.ID }) %>

But for some reason that syntax doesn't work in this code:

<%
  if (Model.CanDoSomething)
    Html.ActionLink("Do Something", "DoSomething", new { id = Model.ID });
%>

I really am a newbie to ASP.NET, so I don't even know what the semantic name is for the different syntaxes <% and <%:; all I can tell is that <% is to void as <%: is to string. And clearly executing a line of code that just returns a string (Html.ActionLink()) is not going to have any effect. But what, pray what is the correct method to make my page render the action link?

It's a great pity I can't Google on "<%"! Any links or explanations of this subject will also be much appreciated.

2 Answers 2

1

This will do the trick

<% if (Model.CanDoSomething) { %>
   <%: Html.ActionLink("Do Something", "DoSomething", new { id = Model.ID }) %>
<% } %>

<%: writes to the output buffer but encodes the string. You could also use <%= for unencoded output because ActionLink returns an encoded MvcHtmlString.

EDIT: This may also work

<%
  if (Model.CanDoSomething)
    Response.Write(Html.ActionLink("Do Something", "DoSomething", new { id = Model.ID }));
%>
Sign up to request clarification or add additional context in comments.

4 Comments

Sure it would, and I worked that out myself. But surely there's a more elegant way of handling this kind of situation?
@Shaul. Yes. Using Razor view engine instead. :)
You can switch to MVC 3 with Razor view engine. This one has a far more intuitive syntax.
Ah, just saw your edit with Response.Write. That's exactly what I was looking for, thanks!
1

<% - this by itself has no output. You would include code with no output in this block such as an if statement. If you want output - you must use it in conjunction with <%=
The difference with the : is that <%: means it will output to the response stream, not = required however the : means the output will be htmlencoded.

<%:"sometest&text" %>   //will emit "sometesttext" on the page.. htmlencoded.
<%="sometest&text" %> //will give you the same result without the '&' htmlencoded
<% SomeFunction() %> //will just run that function - there is no output
//you want
<%if (Model.CanDoSomething){%>
    <%:Html.ActionLink("Do Something", "DoSomething", new { id = Model.ID })%>
<%}%>

2 Comments

+1 for the explanation, if not exactly the solution I was looking for - thanks!
thanks : ) how did it differ? Personally - I wouldn't use the Response.Write syntax. Its going to be a tad more work if you refactor going to razor syntax. There is also no need to use the htmlencoding colon syntax there either, since <%: HtmlEncodes, and the ActionLink helper already spits out type MvcHtmlString which says 'don't encode me'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.