Best method to display all images using a multi-media picker
Hi,
I have added a multiple media picker to my content page. I
can access all the ids for the items selected
using @Umbraco.Field("strainImage") and all that returns me a
list of ids. What would be the best method to retrieve the items which I
can loop through? I would like to get the URLS preferably in a method to loop
through.
What you need is to create a partal view or a partial view macro then you can loop through all the images that has been picked in the multi-media picker. You can choose the write dynamic razor or strongly typed razor, both version will works whatever you choose to use partal view or a partal view macro.
In my example I have choosen to create a partial view macro, and choose the dynmaic razor version. With this code you will get the images that are selected on the page that you are viewing in the browser.
@inherits Umbraco.Web.Macros.PartialViewMacroPage @if (CurrentPage.HasValue("strainimage")) { var caseStudyImagesList = CurrentPage.strainimage.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries); var caseStudyImagesCollection = Umbraco.Media(caseStudyImagesList);
If you rather want to use partial views, then go to the settings section into the folder partial views, and create a new file give it a name, and past in the code above, as I said before you can use the code in both situations whatever if you choose to use partal view or a partal view macro.
The difference between the partial view and a partial view macro is where it @inherits from, the partial view are inherit from:
@inherits UmbracoTemplatePage
Where the parial view macro are inherit from:
@inherits Umbraco.Web.Macros.PartialViewMacroPage
When you have created your razor file and pasted in the code, you need to add the file into your template where you need the images to display.
If you choose the partial view macro you need to add this to the template
@Umbraco.RenderMacro("Mediapicker")
In my example I have named my razor file Mediapicker. If you see the documentation I have linked to higher up it will also explain it. For the partial view you need to add this into your template where the images should display:
Best method to display all images using a multi-media picker
Hi,
I have added a multiple media picker to my content page. I can access all the ids for the items selected using @Umbraco.Field("strainImage") and all that returns me a list of ids. What would be the best method to retrieve the items which I can loop through? I would like to get the URLS preferably in a method to loop through.
Hi Gurjot,
What you need is to create a partal view or a partial view macro then you can loop through all the images that has been picked in the multi-media picker. You can choose the write dynamic razor or strongly typed razor, both version will works whatever you choose to use partal view or a partal view macro.
In my example I have choosen to create a partial view macro, and choose the dynmaic razor version. With this code you will get the images that are selected on the page that you are viewing in the browser.
If you are not familiar with how to create a partial view or partial view marco here a some documentation on how to create a parital view macro http://our.umbraco.org/documentation/Using-Umbraco/Creating-Basic-Site/Articles-Parent-and-Article-Items
If you rather want to use partial views, then go to the settings section into the folder partial views, and create a new file give it a name, and past in the code above, as I said before you can use the code in both situations whatever if you choose to use partal view or a partal view macro.
The difference between the partial view and a partial view macro is where it @inherits from, the partial view are inherit from:
@inherits UmbracoTemplatePage
Where the parial view macro are inherit from:
@inherits Umbraco.Web.Macros.PartialViewMacroPage
When you have created your razor file and pasted in the code, you need to add the file into your template where you need the images to display.
If you choose the partial view macro you need to add this to the template
In my example I have named my razor file Mediapicker. If you see the documentation I have linked to higher up it will also explain it. For the partial view you need to add this into your template where the images should display:
Again I have choosen to call my partial view file media picker. As you probably already has discovered Umbraco comes with some pre-defined data types, including the multi-media picker, try to see this documentation about the property editors in Umbraco 7 http://our.umbraco.org/documentation/using-umbraco/backoffice-overview/property-editors/Built-in-Property-Editors-v7/.
As you can see there a some documentation http://our.umbraco.org/documentation/using-umbraco/backoffice-overview/property-editors/Built-in-Property-Editors-v7/Media-Picker for the media picker and how to pull out the images, and there are shown both razor types that I described eailer in this post. The dynmaic one and the strongly typed razor.
Hope this helps and make sense,
/Dennis
Hi Dennis,
In a V7.2.1 site, using your example I just get : 'Umbraco.Web.Models.DynamicPublishedContentList' does not contain a definition for 'Split'
Here's the code:-
Any clue to what's missing/happening would be appreciated. Just worried it's this issue: https://our.umbraco.org/forum/umbraco-7/using-umbraco-7/53022-UmbracoWebModelsDynamicPublishedContentList-does-not-contain-a-definition-for-Any Hopefully not.
Craig
is working on a reply...