erbsoftware
erbsoftware

Reputation: 365

? Operator using MVC Razor syntax

<span>@Model.SelectedOrderTypeName  @Model.Percentage ? @Model.FinancialSupportPercentage % : @Model.Amount $</span>

How do I do the above in Razor syntax? (Basically if Percentage is not null display it otherwise display amount

Upvotes: 0

Views: 1102

Answers (1)

Alfie Goodacre
Alfie Goodacre

Reputation: 2793

You need to use brackets

<span>@Model.SelectedOrderTypeName  @(Model.Percentage != null ? Model.FinancialSupportPercentage + "%" : Model.Amount + "$")</span>

Without the brackets, every time you use @ you are returning something, meaning you will likely be displaying a <span> containing @Model.SelectedOrderTypeName's value, @Model.Percentage's, value a ? literal character, @Model.FinancialSupportPercentage's value, a % literal character, :, @Model.Amount's value and finally a $ literal.

Adding brackets turns this into one return - allowing ternary functions to be written.

Upvotes: 1

Related Questions