当我的数据库没有为我的网上商店中的文章设置图像时,我有一个问题是显示ImageNotFound。我收到错误值不能为null或为空。
调试时,imageArticle为null。我不知道但似乎来自数据库的ImageArticle导致了这个错误。我能以任何方式绕过它吗?
我添加了一个@if条件来检查null,但它没有看到它。
数据库包含描述,价格和数量,但图像路径尚未存储。它设置为null。 (这是因为我想允许用户在网上商店中创建新文章,但也许他还没有为这个新添加的项目提供任何图片。
这是Browse.html
@model IEnumerable<MyWebStore.Models.Product>
@{
ViewBag.Title = "Browse";
}
<h2>Make your Selection</h2>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.ImageArticle)
</th>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th>
@Html.DisplayNameFor(model => model.Price)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@if (item.ImageArticle == null)
{
<img class="img_set_size" src="~/Images/ImageNotFound.jpg" />
} <==== the error occurs here
Else
{
<img class="img_set_size" alt="Image" src="@Url.Content(item.ImageArticle)"
/>
}
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
@Html.ActionLink("Add to Cart", "AddToCart", "Cart", new { id =
item.ProductId }, null)
</td>
</tr>
}
</table>
答案 0 :(得分:0)
尝试这样:
@if (item.ImageArticle == null)
{
<img class="img_set_size" src="@Url.Content("~/Images/ImageNotFound.jpg")" />
}
else
{
<img class="img_set_size" alt="Image" src="@Url.Content(item.ImageArticle)"/>
}