【问题标题】:Profile Image not getting displayed in the child pages in mvc 4配置文件图像未显示在 mvc 4 的子页面中
【发布时间】:2015-10-29 14:47:48
【问题描述】:

我在 layout.cshtml 页面中有一个用户个人资料图片,该图片显示在整个页面中。现在的问题是图像显示在母版页中,但是在导航到子页面时它没有显示。请帮帮我。

登录控制器显示图片:

public FileContentResult DisplayImage(int userId)
{
    byte[] imageByteData = null;
    try
    {
        using (var db = new AdminDb())
        {
            var userImage = (from usr in db.User.AsNoTracking()
                             where usr.UserId == userId
                             select usr.UserImage).FirstOrDefault();
            if (!string.IsNullOrEmpty(userImage))
            {
                imageByteData = System.IO.File.ReadAllBytes(userImage);
                return File(imageByteData, "image/jpeg");
            }
            else
            {               
                imageByteData = System.IO.File.ReadAllBytes(Server.MapPath(Url.Content("~/Content/UserProfileImages/defaultimage.png")));                   
                return File(imageByteData, "image/jpeg");
            }
        }
    }
    catch (Exception)
    {
        return File(imageByteData, "image/jpeg");
    }
}

用于保存路径的用户控制器。(在此我将图像保存到外部文件夹并将路径保存在数据库中)

if (file.ContentLength > 0)
{
    var setting = db.Setting.AsNoTracking().Where(x => x.Code == OtherConstants.UserPhotoPath)
              .Select(x => new
              {
                  SettingId = x.SettingId,
                  SchemaDefinitionId = x.SchemaDefinitionId,
                  Value = x.Value
              }).FirstOrDefault();

    string path = setting.Value;
    //Getting the path + the image name
    var paths = Path.Combine(path + fileName);
    user.UserImage = paths;
    //Checking whether the uploaded file is already exists and if not exists save the details
    if (!System.IO.File.Exists(paths))
    {
        file.SaveAs(paths);
    }

我的 Layout.cshtml 页面(在哪里使用动作控制器)

<img id="imgLogin" class="lblLogin UserProfileDeatils" src="@Url.Action("DisplayImage", "Login", new { userId = Assyst.PanERP.Common.AppSession.Common.UserID })"  />
</div>

【问题讨论】:

  • 您的子页面是否使用相同的布局页面?
  • 你能运行 fiddler 看看是否有调用 API ~/Login/DisplayImage?UserId=> 失败或返回 HttpResponse 状态码为 200?
  • 我检查时发现只有一次布局被调用。

标签: c# asp.net-mvc asp.net-mvc-4


【解决方案1】:

根据您问题中提供的信息,我认为问题是由以下两个原因之一引起的

  1. 您的子页面使用的布局页面不同于 你已经放了个人资料图片
  2. 您的子页面根本没有使用布局页面

您可以通过在返回图像的方法(使用登录控制器)中放置一个断点来确认这一点,并检查当您的子页面加载到浏览器中时是否调用了该方法。如果该方法没有被命中,那么您可以确认问题是由于上述原因之一。

【讨论】:

  • 我的布局页面有什么问题吗,以防动作控制......@@ user1672994 , @@ muhammad Adeel Zahid
  • 我认为在某些情况下更正的 ID 不会传递给相关方法,因此它返回的是默认图像而不是数据库中的图像。请检查传递给该方法的 id 并检查该 id 的图像是否从 db 返回?
猜你喜欢
  • 2014-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多