【问题标题】:Show images from IQueryable in View在视图中显示来自 IQueryable 的图像
【发布时间】:2013-06-01 15:12:45
【问题描述】:

我有一个关于在视图中显示从 mysql 数据库加载的图像的问题。

在我的数据库表“deliverables”中,我有“item_id”、“deliverable_image”和“afstudeerrichting_id”。 "item_id" 和 "afstudeerrichting_id" 是来自其他表的 FK。

我想在 afstudeerrichting_id = ..时显示图像

控制器:

public ActionResult Index()
{
    var model = repository.GetIdsOfImages(1);
    return View(model.ToList());
}
public ActionResult ShowImage(int afstudeerrichtingid)
{
    IQueryable<byte[]> data = repository.GetImages(afstudeerrichtingid);
    var thedata = data.First();
    return File(thedata, "image/png");
}

存储库(我在其中获取图像):

public IQueryable<long> GetIdsOfImages(int afstudeerrichtingid)
{
    return from deliverable in entities.deliverables
    where deliverable.afstudeerichting_id.Equals(afstudeerrichtingid)
    select deliverable.item_id;
}
public IQueryable<byte[]> GetImages(int afstudeerrichtingid)
{
    return from deliverable in entities.deliverables
    where deliverable.afstudeerichting_id.Equals(afstudeerrichtingid)
    select deliverable.deliverable_image;
}

查看:

@foreach(var imgID in Model.DeliverablesIDsList)
{
    <img src="@Url.Action("ShowImage", "Deliverable", new { DeliverableID = imgID })" />
}

在我的视图模型中:

public List<long> DeliverablesIDsList { get; set; }
public int DeliverableID { get; set; }

现在我得到错误:

传入字典的模型项的类型为“System.Collections.Generic.List`1[System.Int64]”,但此字典需要“GDMfrontEnd.Models.DeliverableViewModel”类型的模型项。

我应该在我的 ViewModel 中进行哪些更改?还是我做错了什么?

【问题讨论】:

标签: asp.net asp.net-mvc image view iqueryable


【解决方案1】:

你可以这样做:
1) 将图像ID列表传递给视图,并像这样构建列表

@foreach(var imgId in model.ImgIdsList)
{
<img src="@Url.Action("ShowImage", "Deliverable", new { imageId = imgId })" />
}

2) 在打开此视图的控制器中,只需构建一个 ImgIdsList(可能您需要一个 GetIdsOfImagesWithAfstudeerichtingid(int afstudeerrichtingid),它将返回一个 int 列表)
3)你应该你的 ShowImage 方法 - 不是通过 afstudeerrichtingid,而是图像的唯一 id;当然在这个方法中你应该像 GetImageById(int id) 这样的方法。

【讨论】:

  • 非常感谢您的帮助!我仍然有一个错误。你知道我做错了什么吗? (更新了我的帖子)
猜你喜欢
  • 1970-01-01
  • 2018-07-03
  • 2018-04-05
  • 1970-01-01
  • 1970-01-01
  • 2012-12-01
  • 2019-07-30
  • 2013-08-02
  • 1970-01-01
相关资源
最近更新 更多