Reputation: 2155
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
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
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