CF西西的博客 | All posts tagged 'NET-MVC3-Razor-if-else'

.Net MVC3 Razor if else 怎么写

14. 二月 2013
参考: 简单的可以:   @if(2<1) { <p>1</p> } else { <p>2</p> }   对比MVC2怎么转换: In ASP.NET MVC2, if we wanted to write some conditional view code (such as displaying a link if a user is logged in), we could do it like so:   <% if (User.Identity.IsAuthenticated) { %> <%= "Your XYZ is " + ... %> <% } else { %> <%= "[not logged in]" %> <% } %>   How do we “translate” this into a Razor-style if-statement? Very simply:   @if (User.Identity.IsAuthenticated) {   Your XYZ is ... } else {   [not logged in] }   What’s cool about this is a complete lack of <% and %>! This is precisely what Scott Guthrie mentioned in his introduction to MVC3 that Razor enables a very fluid coding flow. You don’t need to stop and think about where to write angle brackets; you just write code, write presentation logic, and it works!

.Net MVC