views:

1612

answers:

2

When using the Html helpers for ASP.NET MVC I need to wrap them in a Response.Write else they don't appear. However the samples (1&2 for example) I find online for ASP.NET MVC don't seem to do that. Did something change somewhere or am I doing something wrong?

From the samples I find it should be like this:

<div class="row">
  <% Html.ActionLink("View", "Details", "People"); %>
</div>

However that displays nothing, so I need to wrap it in a Response.Write as follows:

<div class="row">
  <% Response.Write(Html.ActionLink("View", "Details", "People")); %>
</div>
+11  A: 

You need to write them like this:

<div class="row">
    <%= Html.ActionLink("View", "Details", "People") %>
</div>

Note the <%= before the Html.ActionLink. This writes the value into the response.

Praveen Angyan
+8  A: 

Html.ActionLink does not write anything to the response stream. It just returns a string. To output that in the response you need to use Response.Write:

<% Response.Write(Html.ActionLink("View", "Details", "People")); %>

or alternatively, there's a shorthand for Response.Write:

<%= Html.ActionLink("View", "Details", "People") %>

Note that the latter syntax requires an expression rather than a statement, thus it shouldn't have semicolon.

Mehrdad Afshari