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

C#.NET Core

Please contribute by voting. Thanks!

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. Edited Feb 8.

Your comment

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


Created by Peter Wright [8] Feb 8

Share article

Do you know about


Write an article