简体   繁体   English

MVC 5 Razor Url.Content(null)

[英]MVC 5 Razor Url.Content(null)

I've for this mvc5 razor code that is giving error 我为这个mvc5剃刀代码提供了错误

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

The error is Object cannot be null or empty when Model.ImageThumbSrc == null Model.ImageThumbSrc == null时,错误是Object cannot be null or empty

I need to be able to display src="" when Model.ImageThumbSrc == null Model.ImageThumbSrc == null时,我需要能够显示src=""

I tried several ways with ?? 我试过几种方法?? and @{ } but cannot get razor syntax to compile. @{ }但是无法获得razor语法来编译。

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: 你的HTML:

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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