【问题标题】:how to pass null values to url.content() and show result如何将空值传递给 url.content() 并显示结果
【发布时间】:2017-01-05 15:30:03
【问题描述】:

我想使用 url.content() 在 MVC 中使用 img 标签显示图像。我成功了,但是当有一些空图像时它会显示一个“参数异常”错误,所以我也想显示空图像。这是我的代码。

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

当它有价值但我也想要空图像时它会成功。

【问题讨论】:

  • 只需将img 标签包装在if 条件中,例如if (!string.IsNullOrWhitespace(item.image))

标签: c# jquery asp.net asp.net-mvc asp.net-mvc-4


【解决方案1】:

假设“空图像”为 HTTP 404 请求,您可以执行以下操作

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

并将“~/empty-image.jpg”替换为指向您网站上托管的适当图像的有效 URL。

否则,如前所述,只需使用 string.IsNullOrEmpty 方法将您的代码包装到 if 语句 中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-04
    • 2023-03-16
    • 2014-01-16
    • 1970-01-01
    • 2017-06-26
    • 2019-10-31
    • 1970-01-01
    • 2015-11-11
    相关资源
    最近更新 更多