Search code examples
c#model-view-controllerrazor-2umbraco7

Umbraco 7 get media property value


I'm new to Umbraco 7 and MVC. I have added a property called 'teaser' to the media file type. When I upload a media file, the back office interface recognizes the new property and allows me to set its value. I am, however, unable to figure out how to access that value for use in the interface. Here's the code:

@if (CurrentPage.HasValue("audioFiles")) {
var audioIdList = CurrentPage.audioFiles.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
var audioList = Umbraco.TypedMedia(audioIdList);
<ul class="audioFileList"> @{
foreach (var af in audioList) {
        <li>
            <a href="@af.Url">@af.Name</a><br />
            @af.teaser;
        </li>
    }
}
</ul>

}

When I run this code, it throws an error saying that "'Umbraco.Web.Models.PublishedContentBase' does not contain a definition for 'teaser'". The Url and the Name are retrieved just fine. It's only the added 'teaser' that's a problem. Thanks - Jon


Solution

  • try this:

     @af.GetPropertyValue("teaser")
    

    You can only use af.teaser if you are using the dynamic "CurrentPage" object. In this case you inherit from a TypedList which gives you strongly typed .Net objects. These do not contain your custom properties.

    If you like the dynamics more, you should replace var audioList = Umbraco.TypedMedia(audioIdList); by var audioList = Umbraco.Media(audioIdList);. This will give you a dynamic object.