razorif-statementwebmatrix

Using if statements in Razor/Webmatrix


I current have a foreach statement, which shows a group of images using a URL stored within the database.

Some of the records, however, haven't been assigned a picture yet, so I want any records with a missing URL to show a generic image.

Here's the code I currently have:

@foreach(var row in qPropertyDetails) {
    <div class="container">
        <div class="row">
            <div class="span12" style="padding-bottom: 10px">
                <div class="row thumb-pad">
                    <div class="span4">
                        <img src="@row.PrimaryImage" alt="">
                    </div>
                </div>
            </div>
        </div>
    </div>
}

I'm guessing I need an IF statement within the foreach loop, but I don't know the correct syntax. I'm guessing maybe it's something like:

@foreach(var row in qPropertyDetails) {
    <div class="container">
        <div class="row">
            <div class="span12" style="padding-bottom: 10px">
                <div class="row thumb-pad">
                    @if (@row.PrimaryImage=Null) {
                        <div class="span4"><img src="~\tempimage.jpg" alt=""></div>
                    }
                    else {
                         <div class="span4"><img src="@row.PrimaryImage" alt=""></div>    
                    }
                </div>
            </div>
        </div>
    </div>
}

But that doesn't work.


Solution

  • Proper syntax to check for null would be: @if (@row.PrimaryImage == null) {