简体   繁体   中英

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.

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" />

或者你可以使用这个inlne版本:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM