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 == true ? "This will be an exciting party with you" : "So sorry. You'll lose the beeer")
and this is not:
@{Model.WillAttend == true ? "This will be an exciting party with you" : "So sorry. You'll lose the beeer"}
Parentheses are for separating citations or other asides from the body text. Brackets show changes within quoted material. Braces —sometimes known as curly brackets—are not typically used except in technical and mathematical writing.
Curly brackets {} Curly brackets, also known as braces or curly braces, are rarely used in formal writing and are more common in other fields such as science, math, and computing.
Curly brackets are rarely used in prose and have no widely accepted use in formal writing, but may be used to mark words or sentences that should be taken as a group, to avoid confusion when other types of brackets are already in use, or for a special purpose specific to the publication (such as in a dictionary).
The curly brackets are used to denote a block of code in a function. So, say we need a function to calculate the standard error we might do this. The square brackets are used to subset vectors and data frames.
The paren is just an explicit expression, and you will notice that you do not need a semi-colon. The brackets are a code block, to be used just like any other piece of code. Expressions have their output submitted as part of the HTML, whereas code blocks do not.
Phil Haack actually wrote a good primer on some Razor syntax
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";}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With