Ternary operator in Razor Pages / ASP.NET Core and C#

C#.NET Core

Please contribute by voting. Thanks!
1

Ever tried to use the ternary operator (predicate ? if_true : if_false) in .NET core Razor Pages and gotten unexpected result?

This won't work:

<span>@Model.SomeBoolProp ? "Yes" : "No"</span>

.

It's an easy fix - just encapsulate the whole statement in parenthesis and it will work as expected:

<span>@(Model.SomeBoolProp ? "Yes" : "No")</span>

Article created: Feb 8 at 07:40. Edited Feb 8 at 07:41.

Your comment

You need to sign up / log in to comment this article

Author

Created by Peter Wright [8] Feb 8 at 07:40

Share article

Do you know about

git?

Write an article