Ahasanul Banna
Ahasanul Banna

Reputation: 123

How to set default image in MVC Razor syntax

I try this but not work

  @if (PortalContext.CurrentUser.ImageUrl != null)
  {
    <img src="@Url.Content(PortalContext.CurrentUser.ImageUrl)" alt="Picture" style="width: 160px; height: 160px;" />
  }
  else
  {
   //This Image show, when "PortalContext.CurrentUser.ImageUrl" image not view
   < img src="~/images/user-default-image.png" alt="Picture" style="width: 160px; height: 160px;" />
  }

If "PortalContext.CurrentUser.ImageUrl" content image path but anyhow image not preview then default image show. How to do this?

Upvotes: 1

Views: 1050

Answers (1)

Hadi Amini
Hadi Amini

Reputation: 41

In Razor Page :

src="@(PortalContext.CurrentUser.ImageUrl == null ? Href("~/assets/images/PhotoDefaultProfile.png") : PortalContext.CurrentUser.ImageUrl

Upvotes: 1

Related Questions