Paceman
Paceman

Reputation: 2155

MVC 5 Razor Url.Content(null)

I've for this mvc5 razor code that is giving error

 <img class="img-rounded thumbnail-upload" src="@Url.Content(@Model.ImageThumbSrc)" />

The error is Object cannot be null or empty when Model.ImageThumbSrc == null

I need to be able to display src="" when Model.ImageThumbSrc == null

I tried several ways with ?? and @{ } but cannot get razor syntax to compile.

How can I get this to work? This should be simple but I just cant get it.

Upvotes: 1

Views: 1687

Answers (2)

Yacoub Massad
Yacoub Massad

Reputation: 27861

Or you can use this inlne version:

<img class="img-rounded thumbnail-upload" src="@(Model.ImageThumbSrc == null ? "" : Url.Content(Model.ImageThumbSrc))" />

Upvotes: 1

dotnetom
dotnetom

Reputation: 24901

You can use a code block to create a temporary variable and use it:

@{
    var imageSource = Model.Question == null ? "" : Url.Content(Model.Question);
}

And your HTML:

<img class="img-rounded thumbnail-upload" src="@imageSource" />

Upvotes: 2

Related Questions