Try our conversational search powered by Generative AI!

Displaytemplate just renders links

Vote:
 

Dear friends,

I've been trying to implement a display template for images and then use this template in a block. The problem is that no images is rendered, just the url to the image.

 

I've created a newsblock with a image property like this:

[UIHint(UIHint.Image)]
public virtual ContentReference Image { get; set; }

 

My NewsblockController looks like this:

public class NewsBlockController : ActionControllerBase, IRenderTemplate<NewsBlock>
    {        
        public ActionResult Index(NewsBlock currentBlock)
        {
            var model = new NewsBlockViewModel(currentBlock.Heading, currentBlock.MainIntro, currentBlock.NewsRoot, currentBlock.Image);

            return PartialView(model);
        }

    }

And my NewsblockView looks like this:

@model HiQAllehanda.Models.ViewModels.NewsBlockViewModel
 

<div class="newsblock">
          <h1>@Html.PropertyFor(x => x.Heading)</h1>
          @Html.PropertyFor(m => m.Image

</div>

In my Views folder i have created a new folder called DisplayTemplates and in this folder a Image.cshtml file that looks like this:

@model EPiServer.Core.ContentReference

@if(Model != null)
{
    <img src="@Url.ContentUrl(Model)" alt="" />
}

#85528
Apr 29, 2014 10:11
Vote:
 

You can define your property like this:

[UIHint(UIHint.Image)]
public virtual Url Image { get; set; }

    

And use the following code in your DisplayTemplate:

@model EPiServer.Url
@if (Model != null)
{
    <img src="@Url.ContentUrl(Model)" alt="" />
}

    

Hope this helps

#85530
Apr 29, 2014 10:32
Vote:
 

I still just get the link to the image :(

#85533
Apr 29, 2014 11:12
Vote:
 

Try moving your display templates to the /Views/Shared folder

#85535
Apr 29, 2014 11:40
Vote:
 

Now i've tried to move my Image.cshtml file to /Views/Shared folder but still just the link.

#85543
Apr 29, 2014 12:48
Vote:
 

It should be /Views/Shared/DisplayTemplates/Image.cshtml

#85547
Apr 29, 2014 12:55
Vote:
 

That's what i had from the beginning.

#85549
Apr 29, 2014 13:03
Vote:
 

In my Views folder i have created a new folder called DisplayTemplates and in this folder a Image.cshtml file that looks like this:

Are you sure? Seems to me that you did not put it in the "shared" folder, but maybe I misunderstood. 

#85558
Apr 29, 2014 13:12
Vote:
 

I've come across something similar and it looked to me as if image renderers are only used for rendering MediaTypes (ie images) when they are used in Content Areas. Using the EPiServer PropertyFor helper / EPiServer Property control with an Image property didn't use the custom rendering tempate. I'm not sure whether this is a bug or by design.

Try dropping your image in a content area and see what happens.

#85566
Apr 29, 2014 16:06
Vote:
 

It was me that was unclear when writing, my DisplayTemplates folder was under the Shared folder.

#85579
Apr 29, 2014 16:40
Vote:
 

Mark - What do you mean by dropping the image in a content area?

#85581
Apr 29, 2014 16:45
Vote:
 

Have you added a UiHint to the image property on your NewsBlockViewModel?

#85584
Apr 29, 2014 18:42
Vote:
 

By that I mean - add a content area property to your NewsBlock and add the content area to the NewsBlock View. Then using the edit UI add the Image to the content area in your NewsBlock by dragging and dropping

#85586
Apr 29, 2014 19:26
Vote:
 

Per Magne - Not on the NewsBlockViewModel.

 

My NewsBlock.cs looks like this:

 [UIHint(UIHint.Image)]
 public virtual Url Image { get; set; }

 

My NewsBlockViewModel looks like this:

public string Heading { get; set; }
public string MainIntro { get; set; }
public ContentReference NewsRoot { get; set; }
public IEnumerable<PageData> NewsPages { get; set; }
public Url Image { get; set; }

public NewsBlockViewModel(string heading, string intro, ContentReference contentReference, Url image)
  {
            
     Heading = heading;
     MainIntro = GetSubString(intro);
     NewsRoot = contentReference;
     NewsPages = GetNewsPages();
     Image = image;            
   }

#85602
Apr 30, 2014 10:05
Vote:
 

You should add it to the view model as well. Otherwise it will not know which display template to load, and will load the default one, which just displays the url in this case

#85603
Edited, Apr 30, 2014 10:08
Vote:
 

Great, adding the UiHint to the viewmodel did the trick! Thanks to all of you for helping me!

#85608
Apr 30, 2014 11:27
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.