Josh
Josh

Reputation: 1970

Null-Coalescing Operator in Razor View

I have the following expression which can be an empty string in some cases

@item.Creator

And I tried to represent it as follows in Razor View page but it is not giving me what I expect. This is how I wrote the code

@item.Creator ?? Unknown

Expecting that in cases where the Creator is an empty string that I would get "Unknown" in its place. However, on the contrary, I get the following:

?? Unknown

in the page as output. I am obviously missing something on how to apply this operator in Razor View and I will appreciate any guide to correct it.

I am working with ASP.NET-Core 3.1 on C# 8 on a windows machine thank you

Upvotes: 2

Views: 1668

Answers (1)

Klaycon
Klaycon

Reputation: 11080

Per MSDN:

With the exception of the C# await keyword, implicit expressions must not contain spaces.

Use an explicit expression instead:

@(item.Creator ?? "Unknown")

Upvotes: 4

Related Questions