Difference between ( ) (parenthesis) and { } (curly brackets) in Razor

前端 未结 3 726
孤街浪徒
孤街浪徒 2021-02-05 03:51

What is the difference between them both. I thought they were the same but today I realized that they are not.

Why this is correct

@(Model.WillAttend ==         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-05 04:39

    To the question: "why is the second one not valid?", in addition to what Betty and Justin say, the issues specific to what you show: inside curly braces you need your code to follow the normal syntax of c#, so you can't have just a loose "a==b?c:d", without assigning the result to something. And you need a semicolon. So you could say

    @{string message = Model.WillAttend == true ? 
         "This will be an exciting party with you" : 
         "So sorry. You'll lose the beeer";}
    

提交回复
热议问题