0
votes

I have an Archetype property editor set up with multiple fieldsets, each fieldset with 2 fields: a text box, and a media picker for image.

In a GetImage(string imgId){} method I'm trying to get the fieldset with the image url and text box value based on the imgId parameter from the media picker, and to return a htmlString. Not sure if this the best idea, I'm open to suggestions.

public static string GetIcon(string imgId)
{
    var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
    var myProperty = (IPublishedContent)umbracoHelper.Content(5369).GetPropertyValue<ArchetypeModel>("myProperty");
    return "";
}

To get all the items from property I would do:

foreach (var item in myProperty)
{
    var imgId = item.GetValue("img") //get the image id
    var img = umbracoHelper.TypedMedia(imgId);
    <img src="@img.Url" />
    <span>@item.GetValue("imgTitle")</span> 
}

I noticed while debugging that fieldsets within archetype property don't have built in unique ID's ???

How can I get the image and textBox value by comparing the image Id from media picker?

Something like: ??

var icon = myProperty.Where(x => x.icon == iconId); //wrong
1

1 Answers

0
votes

I just played with this too. What I did was:

var helper = new UmbracoHelper(UmbracoContext.Current);
var media = helper.TypedMedia(iconId);
var imageUrl = media.GetPropertyValue<string>("umbracoFile");

This will return a URL like this: "/media/1001/1.jpg". This way you can have a much cleaner view with:

foreach (var item in myProperty)
{
    <img src="@item.imageUrl" />
    <span>@item.GetValue("imgTitle")</span> 
}