Display Name
Display Name

Reputation: 15091

How to render ViewBag.Message when it is not null but without a temporary variable?

I can render ViewBag.Message in a Razor view as follows.

<h1>Message: @{string message = ViewBag.Message?? "Empty";} @message</h1>

Is there any shorter way to do so without using a temporary variable message?

Upvotes: 0

Views: 21

Answers (1)

SpruceMoose
SpruceMoose

Reputation: 10320

Try using an explicit expression with parenthesis:

<h1>Message: @(ViewBag.Message ?? "Empty")</h1>

Upvotes: 1

Related Questions