Thursday, March 3, 2011

In MVC, when do you use <%= %> and <% %>?

Hi,

In asp.net mvc, when do we use:

<%= %>

and

<% %>

Do we ever need to put a ; (colon) ?

From stackoverflow
  • <%= %> renders the output (string) of the contained command to the response. <% %> wraps executable statements (logic) in the view to control what gets executed. You don't use semicolons in the <%= %> blocks, but may in the <% %> depending on what statements are included.

    String rendering:

    <%= Html.Encode( Model. Property ) %>

    Code block:

    <% Html.RenderPartial( "ViewName" ); %>

    EDIT: Here's a link to the reference.

    Codewerks : Correct. Same rules as in ASP.NET and ASP Classic apply.
  • <%="something" %> is just a shortcut for Response.Write("something")

  • <%= %> is used when you are calling some HtmlHelper method which returns a string e.g.:

    <%= Html.ActionLink("Home", "Index", "Home") %>
    

    <% %> is used when you are calling some HtmlHelper method which is void:

    <% Html.RenderPartial("Login"); %>
    

0 comments:

Post a Comment