cv-MvC视图中不存在源时如何显示默认图像

我在loginpartial.cshtml文件中有以下代码
谁能建议如果src不存在,是否可以显示默认图像?

@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>

}

解决方法:

将默认图片添加到您的Web服务器,即myDefaultImage.png.
将ImageName和Photo属性添加到模型中

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

如果图像存在,请从您的控制器设置ImageName属性.如果未设置,则“获取照片”属性将处理默认图像.

然后,您可以显示src,如下所示:

src="/user/RetrievePhoto/@Model.Photo"
上一篇:CodeGo.net>如何显示图像从MVC 4中的路径?


下一篇:NET Razor Bootstrap如何将很长的元素声明拆分为多行代码