Reputation: 2711
I got some code getting data from a database. Sometimes, there is also an image among that data which i fetch like this:
<img src="@Model.Pictue.Url">
The problem is that an image does not always exist in the DB which generates a null-exception. Can I maybe create an if-statement checking if there is an image and if its not, step-over the image-part?
Or is there a better way to solve it?
Upvotes: 0
Views: 1568
Reputation: 23
Try this:
@if(Model.Picture != null)
{
<img src="@Model.Pictue.Url">
}
Upvotes: 2
Reputation: 6398
try this..You can put dummy picture if you dont have
@if(Model.Picture.Url == null)
{
<img src="/DummyPicture">
}
else
{
<img src="@Model.Pictue.Url">
}
Upvotes: 1
Reputation: 62488
do like this:
@if(Model.Picture != null)
{
<img src="@Model.Pictue.Url">
}
Upvotes: 2