Search code examples
asp.net-mvcmodel-view-controllersitecoreglass-mappersitecore-mvc

can we render image sitecore mvc view using itemID


In my MVC view i'm able to get Image ItemID now want to render it i'm doing this as:

   // Getting ImageItemID
   ID myImageItemId = new ID(image.Substring(image.IndexOf('{', 0), 38));
   // Getting ImageItem according myImageItemId 
   Item myImageItem = db.GetItem(myImageItemId);
   @Html.Raw(myImageItem)
   <img src="@myImageItem"/>

please someone can hep to what mistake i'm doing here


Solution

  • You need a dedicated action that will return the contents of the actual image by it's id:

    public ActionResult Image(int imageID)
    {
        byte[] image = SomeService.GetImageBytes(imageID);
    
        return File(image, "image/jpeg");
    }
    

    After that you can use this action in your view:

    <img src="@Url.Action("Image", "SomeController", new {imageID = myImageItem})"/>