Im using this in my view and want it to display only "Yes" or "No"
but its displaying False?"yes":"No"
@myPosts.Contains(item.ID)?"Yes":"No"
Whats wrong here?
The If Condition The if statement returns true or false, based on your test: The if statement starts a code block. The condition is written inside parenthesis. The code inside the braces is executed if the test is true.
Razor is a markup syntax that lets you embed server-based code into web pages using C# and VB.Net. It is not a programming language. It is a server side markup language. Razor has no ties to ASP.NET MVC because Razor is a general-purpose templating engine.
If you are trying to check if model is empty which is not a list and may contain single record or none then I normally use @if(Model == null) .
What is Razor? Razor is a markup syntax that lets you embed server-based code (Visual Basic and C#) into web pages. Server-based code can create dynamic web content on the fly, while a web page is written to the browser.
You need parentheses to use an expression:
@(myPosts.Contains(item.ID)?"Yes":"No")
You can even nest shorthand if inside of another shorthand if!
@(myPosts != null ? (myPosts.Contains(item.ID) ? "Yes" : "No") : "Null")
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