0

我有一个关于在视图中显示从 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 中更改什么?或者我做错了什么?

4

1 回答 1

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) 之类的方法。

于 2013-06-05T19:27:39.537 回答