2

在 ASP.NET MVC 中,我尝试使用以下代码将模型字段与 displayText 绑定:

<%    if (Model.WillAttend == true)
           Html.DisplayTextFor(x => x.Name);   %>

但是当我尝试时:

<%    if (Model.WillAttend == true)     %>  
         <% = Html.DisplayTextFor(x => x.Name)     %>

它正在工作,为什么?两者似乎相同的代码,唯一的不同是在下面一个只是每一行都用服务器端标签分隔。

4

1 回答 1

2

The difference is in the = sign after the open tag <%. This ensures that the value is written to the output. The first example is simply declaring a value and not doing anything with it.

Check out this blog entry for more info on the ASPX view engine tag syntax.

This is simplified a lot with Razor syntax, where you are able to just prefix a line in a codeblock with @ in order to write it to output. I don't know if there is a similar functionality in the ASPX view engine though.

于 2013-07-05T07:49:15.600 回答