user2915962
user2915962

Reputation: 2711

If image = null, do nothing

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

Answers (3)

user3547697
user3547697

Reputation: 23

Try this:

@if(Model.Picture != null)
{
<img src="@Model.Pictue.Url">
}

Upvotes: 2

Nilesh Gajare
Nilesh Gajare

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

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

do like this:

@if(Model.Picture != null)
{

<img src="@Model.Pictue.Url">
}

Upvotes: 2

Related Questions