0

我有一个 MVC 视图,它有几个图像(每页 1 个)。

例子:

<img alt ='thumb1' src='<%= Model.ThumbnailURL %>' height="65" width="90" />

问题是模型可能有也可能没有 ThumbnailURL,这会导致对象引用未设置消息。

当图像不存在时如何防止它们加载。我不想使用默认图像

4

2 回答 2

1

在它周围加上一个 if 语句。

<% if (!string.IsNullOrEmpty(Model.ThumbnailURL)) { %>
<img alt ='thumb1' src='<%= Model.ThumbnailURL %>' height="65" width="90" />
<% } %>

编辑:更改为 WebForms 视图引擎,我假设您使用的是 Raser。

于 2012-07-19T01:01:04.663 回答
0

要绕过NRE(空引用异常),请使用空合并运算符 ??

<img alt ="thumb1"
     src="<%= Model.ThumbnailURL ?? "" %>"
     height="65"
     width="90" />
于 2012-07-19T00:48:06.447 回答