简体   繁体   English

如何将空值传递给url.content()并显示结果

[英]how to pass null values to url.content() and show result

I want to display images using url.content() in MVC using an img tag. 我想使用img标签在MVC中使用url.content()显示图像。 I succeeded, but it displays an 'argument exception' error when there are some empty images, so I want to show null images also. 我成功了,但是当有一些空图像时,它会显示“参数异常”错误,因此我也想显示空图像。 Here is my code. 这是我的代码。

<img src="@Url.Content(item.image)" alt="Image" height="50" width="50"  class="img-circle img-responsive" />

It succeeds when it has value but I want empty images also. 当它有价值时,它会成功,但是我也想要空图像。

Assuming "empty images" as HTTP 404 requests, you can do the following 假设“空白图片”为HTTP 404请求,则您可以执行以下操作

<img src="@Url.Content(item.image ?? "~/empty-image.jpg")" alt="Image" height="50" width="50"  class="img-circle img-responsive" />

And substitute "~/empty-image.jpg" with a valid URL pointing to an appropriate image hosted on your website. 并用指向您网站上托管的适当图像的有效URL替换“〜/ empty-image.jpg”。

Otherwise, as other mentioned, just wrap your code into an if-statement using string.IsNullOrEmpty method. 否则,就像其他提到的那样,只需使用string.IsNullOrEmpty方法将代码包装到if语句中即可

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

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