1

My model contains string property ImagePath with media file path, for example:

"/sitecore/media library/Comp_Images/Division1/Client1/Client_Images/BobRoss.jpg"

How can I display it into cshtml in sitecore rendering ? something like:

 <img  src="@Model.ImagePath" />

Thank you

UPDATE:

  1. model is coming from database - ImagePath field type is string
  2. images are being uploaded programmaticaly via API from another app.
3
  • 2
    Where does your model get this path from? What type of field do you have in your Sitecore template for this (assume it is Image)
    – jammykam
    May 2 '18 at 20:27
  • 1
    Also have you looked at Glass Mapper? glass.lu this makes this kind of thing a lot easier, more testable and has experience editor support built in. May 2 '18 at 22:21
  • 1
    Since you have the full path, you can use the GetItem() method to get the item. Cast it as mediaItem. Example: var imageItem = (MediaItem)Sitecore.Context.Database.GetItem("path of item here");. Once you have the item, you can use the GetMediaUrl() to generate the image url. Then you store it in your property ImagePath May 3 '18 at 10:17
3

Since you already have the media path in Sitecore, you can use the GetItem() method to retrieve the item.

Below is the code snippet you can use to get the url of the media and assign it to your property ImagePath

var imageItem = Sitecore.Context.Database.GetItem("path of item here");
this.ImagePath = MediaManager.GetMediaUrl(imageItem);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.