Try our conversational search powered by Generative AI!

On page editing for media types?

Vote:
 

I might be missing something basic here but if I have a pagetype like this:

    [ContentType(DisplayName = "Artikelsida", GUID = "168eb06f-d640-4a36-ae9b-13a53f2c16ed", GroupName = Global.PageTypeGroupName.Default)]
    public class ArticlePageType : BaseContentPage
    {
        public virtual ContentReference Image { get; set; }
    }

And I choose an image that is of this media type:

    [ContentType(GUID = "0A89E464-56D4-449F-AEA8-2BF774AB8730")]
    [MediaDescriptor(ExtensionString = "jpg,jpeg,jpe,ico,gif,bmp,png")]
    public class ImageFile : ImageData
    {
        public virtual string Copyright { get; set; }

        public virtual string AlternativeText { get; set; }

        [ImageDescriptor(Width = 100, Height = 100)]
        public override Blob Thumbnail
        {
            get { return base.Thumbnail; }
            set { base.Thumbnail = value; }
        }
    }

 

How do I show this in my view and make it editable?

I've been looking in this guide: http://world.episerver.com/Documentation/Items/Developers-Guide/EPiServer-CMS/75/Content/Assets-and-media/Working-with-media/
But it doesn't really show how to display it in a view and making it editable.

The alloy templates has a Controller called ImageFileController but it's not used anywhere in the project and even if it was, it doesn't look like it would be editable.

 

#80054
Jan 14, 2014 14:35
Vote:
 

Add [UIHint(UIHint.Image)] to your property so that it will be recognized as an image. It will now look for a display template called Image. Take a look at the alloy mvc templates and the Image.cshtml file.

#80059
Jan 14, 2014 15:14
Vote:
 

But that will just display it as a img tag with only the src attribute. I want to utilize the AlternativeText and the Copyright properties aswell.

#80060
Jan 14, 2014 15:27
Vote:
 

Ah, I see.
The ImageFileController in the mvc templates will be used if you drop an image into a content area. However, if you want to render it the same way when you use it as a property on a page, I think you will need to create a view model for your page. On the view model you should put a property of type ImageFile, and then fetch the value from the content repository.
You should then be able to use Html.PropertyFor(...) on the ImageFile property to get the desired behavior.

#80061
Jan 14, 2014 15:49
Vote:
 

An easier way to do it:
I made an extension for rendering content references. 

        public static void RenderContentReference(this HtmlHelper html, ContentReference contentReference)
        {
            IContentData contentData =
                ServiceLocator.Current.GetInstance<IContentRepository>().Get<IContent>(contentReference);
            IContentDataExtensions.RenderContentData(html, contentData, false, html.ViewContext.ViewData["tag"] as string);
        }

    

And changed the display template to:

@model EPiServer.Core.ContentReference
@{ Html.RenderContentReference(Model);}

    

It will now use the ImageFileController

#80064
Jan 14, 2014 16:05
Vote:
 

Would you mind giving me an example? I did the following and all i get is an error "No parameterless constructor defined for this object."
Added a ContentReference to my pagetype:
[ContentType(DisplayName = "Artikelsida", GUID = "168eb06f-d640-4a36-ae9b-13a53f2c16ed", GroupName = Global.PageTypeGroupName.Default)]
public class ArticlePageType : BaseContentPage
{
    [UIHint(UIHint.Image)]
    public virtual ContentReference Image { get; set; }
}
Added an imagefile property to my viewmodel och added it in my controller:
public ActionResult Index(ArticlePageType currentPage)
{
       var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();

    var imageFile = contentRepository.Get<ImageFile>(currentPage.Image);
    var model = new ArticlePageViewModel(currentPage)
    {
        ImageFile = imageFile
    };
    
    return View(model);
}

And added this to the view:

@Html.PropertyFor(x => x.ImageFile)

The partial view for the ImageFileControll looks like this:

@model Metamatrix.Standard.Models.ViewModels.ImageViewModel

<img src="@Model.Url" alt="@Model.Name" title="@Model.Copyright" class="image-file" />

#80068
Jan 14, 2014 16:21
Vote:
 

Have you added the StructureMapDependencyResolver? If not, the dependency injection will fail on the ImageFileController (the UrlResolver that is being passed through the constructor).

You have two options: Include the DependencyResolverInitialization and StructureMapDependecyResolver files from the alloy project. Or you could change the ImageFileController to have an empty constructor, and then fetch the UrlResolver using

ServiceLocator.Current.GetInstance<UrlResolver>();

 

#80069
Edited, Jan 14, 2014 16:32
Vote:
 

I used your second suggestion and it  worked! Didn't get any on page editing though so I had to apply @Html.EditAttribute(x => x.CurrentPage.Image). Would you say this is the best practice or?

#80105
Edited, Jan 15, 2014 8:58
Vote:
 

You could try renaming the property name from "ImageFile" to "Image" on your view model. If the property names are the same, EPiServer should automatically be able to map the two properties. I'm not 100% sure it will work since it is two different types though. If it does not work, your approach using EditAttribute is the correct one.

#80135
Jan 15, 2014 14:25
Vote:
 

Thank you for all your help!

#80136
Jan 15, 2014 14:56
Vote:
 

Hello,

I followed the information above. Implemented a RenderContentReference helper. A ImgaeFileController. Call the helper from a View. I have also made different controllers using Tags to handle different scenarios. Everything works fine :) Than you!

No I'm a wodering how I can do to have the same funtionality for a Xhtml property?

#82002
Mar 03, 2014 16:09
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.