1

我目前正在开发的一个 webApp 要求我将照片作为 a 存储byte[]在数据库中。然后它需要我将它们拉出来并在视图中显示它们。我创建了一个photoContainer保存位图而不是保存位图byte[]并将其传递给视图。但我似乎无法弄清楚如何从那里显示它。我已经尝试将源设置为model.Image,但没有奏效。无论如何要在视图中的模型中显示位图吗?

提前致谢。

4

1 回答 1

2

In a controller place this

public FileResult GetImg(int id)
    {                       
         var image = db.Categories.First(m => m.CategoryID == id).Picture;

         byte[] imageData;
         if(image != null)
         {
             imageData = image.ToArray();
             return File( imageData, image.contentType );
         }
         else
         {
             return null;
         }
     }

In your view place this

<img src="@Url.Action("GetImg", "ControllerTheActionAboveIsPlacedIn", new { id = Model.ImageId})" />
于 2012-06-12T03:48:21.043 回答