how to render an page type field that is for an image using the direct upload

lawrence whittemore asked on June 8, 2021 18:21

I am able to get an image to show on the front end of my site using something like this

<img src="~/getattachment/@Model.Page.TeaserImage/@(Model.Page.NodeAlias).jpg" />

But in the admin it doesn't show and generates a different URL.

The page uses content based routing so I don't have any view models or controls associated with it.

Recent Answers


Dmitry Bastron answered on June 8, 2021 19:28

Hi Lawrence,

This is not the right approach for generating the proper attachment URL. Please check out this documentation article, I believe it explains exactly your case how to get the attachment URL to display an image.

0 votesVote for this answer Mark as a Correct answer

lawrence whittemore answered on June 9, 2021 14:06

I couldn't really find a solution. The pages use the content based routing so there isn't any controllers set up for this. Is there a tag helper or something similar I can use?

0 votesVote for this answer Mark as a Correct answer

lawrence whittemore answered on June 9, 2021 21:04 (last edited on June 10, 2021 14:27)

So, I should've said the page type used the Basic content based routing so there were no controllers or models just that they used the @model Kentico.Content.Web.Mvc.Routing.IPageViewModel<Home> in the cshtml page to get the view model.

I could not get it to work so I just built my own model and controller based on the documentation and set it up that way. if there is a way to accomplish what I was trying I'd love to see it, otherwise, this way works fine too.

0 votesVote for this answer Mark as a Correct answer

Dmitry Bastron answered on June 10, 2021 19:00

Hi Lawrence,

I believe this code should work for you in your basic view:

@inject IPageAttachmentUrlRetriever attachmentUrlRetriever

<img src="@attachmentUrlRetriever.Retrieve(Model.Page.TeaserImage)" />
0 votesVote for this answer Mark as a Correct answer

lawrence whittemore answered on June 11, 2021 00:29

I may be missing a using statement but in testing I am getting an error cannot convert from 'System.Guid' to 'CMS.DocumentEngine.IAttachment'

0 votesVote for this answer Mark as a Correct answer

Sean Wright answered on July 6, 2021 23:16

@Lawrence,

The API that Dmitry referenced is this:

//
// Summary:
//     Provides an interface for retrieving the page attachment URL based on given parameters.
public interface IPageAttachmentUrlRetriever
{
    //
    // Summary:
    //     Retrieves the page attachment URL representation for given page attachment.
    //
    // Parameters:
    //   attachment:
    //     Page attachment.
    IPageAttachmentUrl Retrieve(IAttachment attachment);
}

It requires an IAttachment. If you pass the Page Type property accessed through the nested .Fields class, then you will be given a DocumentAttachment, however if you use the Page Type field directly off the Page Type instance, it will be a Guid.

I'm guessing you used the wrong property.

Example:

DocumentAttachment attachment = homePage.Fields.Image;
Guid attachmentGuid = homePage.HomePageImage;
0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.