Damien
Damien

Reputation: 621

MVC @if statements

Just trying to write simple if statement to check whether a model has a certain string value. Currently I have this

@if (offer.Title === "Offer")
{
    //do something
}

I know I have results where the title does equal "Offer" but nothing is displaying on my webpage. I don't normally write MVC (hence the noobie question) but I figured this would be pretty simple so just went for it myself..

Upvotes: 0

Views: 1083

Answers (1)

kayess
kayess

Reputation: 3394

According to the C# reference, the existing equality operators are:

==

By default, for reference types other than string, this returns reference equality (identity test). However, types can overload ==, so if your intent is to test identity, it is best to use the ReferenceEquals method on object.

!=

not equal. See comment for ==. If a type overloads ==, then it must overload !=

So the outcome is that there is no triple equality operator, you have got to use the double equality operator. Note: in other languages the triple equality operator === would also check for the type equality not just the value equality, but since C# is a type safe language there is no need for the triple.

Upvotes: 6

Related Questions