How to display the default image in case the source does not exist in MvC View

I have the following code in my loginpartial.cshtml file
Can anyone advise if there is a way to display the default image if src doesn't exist?

@if (Request.IsAuthenticated)
{   

    <li><a href="#">Support</a></li>
    <li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown" href="#">



        //in below image tab m Display user Uploaded image ,
        //wants to display default Image if no Image is present

        <img src="/user/RetrievePhoto/@ABC.Web.Session.LoginData.UserToken.UserId" alt="" class="profilePhotoSM" />@ABC.Web.Session.LoginData.UserToken.UserName <span class="caret"></span></a>
        <ul class="dropdown-menu">
            <li>@using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm" }))
                {
                @Html.AntiForgeryToken()
                <a href="javascript:document.getElementById('logoutForm').submit()">Log off</a>
                }</li>
        </ul>
    </li>


}
else
{

    <li>@Html.ActionLink("Sign Up", "Register", "User", routeValues: null, htmlAttributes: new { id = "registerLink", @class = "navLink" })</li>
    <li>@Html.ActionLink("Login", "Authentication", "Account", routeValues: null, htmlAttributes: new { id = "loginLink", @class = "navLink" })</li>

}

      

+3


source to share


1 answer


Add the default image to your web server, i.e. myDefaultImage.png

... Add properties ImageName

, and Photo

to your model ie

public class AModel
{
   public string ImageName { get; set; }
   public string Photo { get { return ImageName == null ? "myDefaultImage.png" : ImageName; }}
}

      

Set the property ImageName

from your controller if the image exists. Get on property Photo

will handle the default image if it is not set.



Then you can show src like this:

src="/user/RetrievePhoto/@Model.Photo"

      

+3


source







All Articles