I have the following code to get the images from the Multiple Media Picker in my caroussel:
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@if (Model.Content.HasValue("MultiMediaPicker"))
{
var imagesList = Model.Content.GetPropertyValue<string>("MultiMediaPicker").Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
var imagesCollection = Umbraco.TypedMedia(imagesList).Where(x => x != null);
foreach (var imageItem in imagesCollection)
{
<li>
<img src="@imageItem.Url" />
</li>
}
}
This works fine on the homepage but since the caroussel is the banner of the website it needs to work for every page. So I figured I would change the code appropiately to:
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@if (CurrentPage.AncestorsOrSelf(1).First().HasValue("MultiMediaPicker"))
{
var imagesList = CurrentPage.AncestorsOrSelf(1).First().GetPropertyValue<string>("MultiMediaPicker").Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
var imagesCollection = Umbraco.TypedMedia(imagesList).Where(x => x != null);
foreach (var imageItem in imagesCollection)
{
<li>
<img src="@imageItem.Url" />
</li>
} }
It does go into the if statement on every page but it can't find the MultiMediaPicker. Giving me the following error:
ERROR LOADING PARTIAL VIEW SCRIPT (FILE: ~/VIEWS/MACROPARTIALS/MULTIMEDIAPICKER.CSHTML)
If I use Model.Content is works on the homepage but not on any other pages. As such I am trying to use AncestorsOrSelf() or any other method to get the correct path on every page.
If I use a combination of the two:
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@if (CurrentPage.AncestorsOrSelf(1).First().HasValue("MultiMediaPicker"))
{
var imagesList = Model.Content.GetPropertyValue<string>("MultiMediaPicker").Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
var imagesCollection = Umbraco.TypedMedia(imagesList).Where(x => x != null);
foreach (var imageItem in imagesCollection)
{
<li>
<img src="@imageItem.Url" />
</li>
}
}
It works on the homepage and I get the following error on other pages:
Object reference not set to an instance of an object.
I tried the below code and it works for me. it returns the image ids
@{
var imagelist = @Model.Content.GetPropertyValue("MultiMediaPicker", true);
}
I assume that your case is like.
you have MultiMediaPicker property at root node/home and i made a partial macro as you and then i use the macro in below pages like about us and it works
Can you please explain your scenerio?
Other way is that you can create a pageid parameter in Macro where do you want to get the value.
@Charles, this snippet will not throw any exception. If it's empty then it doesn't "HasValue" so doesn't get past the first "if". There are multiple additional checks in here to avoid different exceptions.
Removing all the checks you would be left with the snippet below, but it would throw exceptions in various situations.
@{
var homepage = Model.Content.AncestorOrSelf(1);
var imagesList = homepage.GetPropertyValue("MultiMediaPicker").Split(',');
var imagesCollection = Umbraco.TypedMedia(imagesList);
foreach (var imageItem in imagesCollection)
{
<li>
<img src="@imageItem.Url" />
</li>
}
}
AncestorsOrSelf()
Hello Everyone,
I have the following code to get the images from the Multiple Media Picker in my caroussel:
This works fine on the homepage but since the caroussel is the banner of the website it needs to work for every page. So I figured I would change the code appropiately to:
It does go into the if statement on every page but it can't find the MultiMediaPicker. Giving me the following error: ERROR LOADING PARTIAL VIEW SCRIPT (FILE: ~/VIEWS/MACROPARTIALS/MULTIMEDIAPICKER.CSHTML)
Help will be greatly appreciated!
Hi,
Can you use like it?
var imageList = Model.Content.GetPropertyValue("MultiMediaPicker", true);
if (imageList!=null){
you code here
}
OR
if (Model.Content.HasValue("MultiMediaPicker", true))
{
}
If I use Model.Content is works on the homepage but not on any other pages. As such I am trying to use AncestorsOrSelf() or any other method to get the correct path on every page.
If I use a combination of the two:
It works on the homepage and I get the following error on other pages: Object reference not set to an instance of an object.
I tried the below code and it works for me. it returns the image ids
@{
var imagelist = @Model.Content.GetPropertyValue("MultiMediaPicker", true);
}
I assume that your case is like.
you have MultiMediaPicker property at root node/home and i made a partial macro as you and then i use the macro in below pages like about us and it works
Can you please explain your scenerio?
Other way is that you can create a pageid parameter in Macro where do you want to get the value.
Yasir
Yasir
Hi Desley,
I would probably do this:
Jeavon
Thanks a bunch. Works perfectly.
You're welcome!
Jeavon will that code will throw if the collection is empty? If it trys to .split on nothing :).
Charlie
@Charles, this snippet will not throw any exception. If it's empty then it doesn't "HasValue" so doesn't get past the first "if". There are multiple additional checks in here to avoid different exceptions.
Removing all the checks you would be left with the snippet below, but it would throw exceptions in various situations.
Jeavon
is working on a reply...