问题描述:

Why do I need a semicolon to end a

<% Html.BeginForm(); %>

but not need a semicolon when ending a

<%= Html.TextBox("Name") %>

网友答案:

Your first example is calling an Html extension method, which has the side effect of writing out content.

Your second example is using =, which is short-hand for Response.Write(). The .TextBox() does NOT have the side effect of writing into the Response, so you have to do it manually with the =.

网友答案:

Code blocks can have many lines of code, so the semicolons are needed.

<% Html.BeginForm();
   DoSomeThingElse();
%> 

When you use <%= %> you are evaluating a single statement and outputting the result.

相关阅读:
Top