How to count number of items in uComponents Multi URL Picker
Hi have a Multi URL pickers set up on a doc type of "Product". The point of the picker is to assign similar or related products to this product and have them display at the bottom of the page.
The alias of the multi url picker is "relatedProducts". The below code works perfectly for checking the item values in the multi pickers and displays the image of the related products on the page, but what I also what to do is count how many items there are before the foreach loop because I dont want the 2 divs and the h1 tag to be diplayed if there are no related products. Can anyone advise how best to do this.
OK, so I have edited my script and the on save error has gone but it still says Error loading macro on the page.
Here is the first few lines of my code.
@using umbraco.MacroEngines
using uComponents.DataTypes.MultiUrlPicker.Dto
@inherits umbraco.MacroEngines.DynamicNodeContext
@{
uComponents.DataTypes.MultiUrlPicker.Dto.MultiUrlPickerState links = null;
var product = Model.AncestorOrSelf("Product");
if(product.HasValue("relatedProducts"))
{
@* Check if the current product has any related products at all*@
links = uComponents.DataTypes.MultiUrlPicker.Dto.MultiUrlPickerState.Deserialize(Model.relatedProducts);
if (links != null && links.Items.Count() > 0)
{ ...
I have Visual Web Developer 2010 installed and have been using that in some cases to write the scripts, but it doesn't show any errors, and the whole site builds without errors.
Here is my full script.
@using umbraco.MacroEngines
@using uComponents.DataTypes.MultiUrlPicker.Dto
@inherits umbraco.MacroEngines.DynamicNodeContext
@{
uComponents.DataTypes.MultiUrlPicker.Dto.MultiUrlPickerState links = null;
var product = Model.AncestorOrSelf("Product");
if(product.HasValue("relatedProducts"))
{
@* Check if the current product has any related products at all*@
links = uComponents.DataTypes.MultiUrlPicker.Dto.MultiUrlPickerState.Deserialize(Model.relatedProducts);
if (links != null && links.Items.Count() > 0)
{
<div class="width1680 popularProducts">
<div class="width960">
<h1 class="centerText">RELATED PRODUCTS</h1>
@foreach (var urlPicker in Model.AncestorOrSelf("Product").relatedProducts.Items)
{
if(urlPicker != null)
{
var linkTarget = (urlPicker.NewWindow) ? "target=\"_blank\"" : String.Empty;
var nodeId = urlPicker.NodeId;
var image = Library.NodeById(nodeId);
<div class="floatProducts">
<div class="sqrProductsImage">
<a href="@urlPicker.Url" @Html.Raw(linkTarget)>
<img src="@image.Media("mainImage","umbracoFile")" />
</a>
</div>
<a href="@urlPicker.Url">
<div class="overlay"></div>
<div class="ic_caption">
<h2 class="centerText">@image.Name</h2>
</div>
</a>
</div>
}
}
<div class="clearLeft"></div>
</div>
</div>
}
}
}
I couldnt figure out how to count the items at all without it erroring, so thought of something else which probably isn't very pretty and if there a lot of related products might take a little while, but for my purposes it should be fine.
How to count number of items in uComponents Multi URL Picker
Hi have a Multi URL pickers set up on a doc type of "Product". The point of the picker is to assign similar or related products to this product and have them display at the bottom of the page.
The alias of the multi url picker is "relatedProducts". The below code works perfectly for checking the item values in the multi pickers and displays the image of the related products on the page, but what I also what to do is count how many items there are before the foreach loop because I dont want the 2 divs and the h1 tag to be diplayed if there are no related products. Can anyone advise how best to do this.
I have tried...
@product.relatedProducts.Items.Count()
&@product.relatedProducts.Count()
with no luck.Any help is appreciated.
Try having a look at this post
Have you also tried @product.relatedProducts.Count() > 2 ?
Hi Fuji,
@product.relatedProducts.Count() does nothing. The script saves but I get an error on the web page just saying there was an error loading the macro.
The link you provided is for the Multi Node Tree Picker which is different to what I'm using, also it doesn't mention how you do any counting. :(
Hi John,
You should use the uComponents model deserialiser
uComponents.Core.DataTypes.MultiUrlPicker.Dto.MultiUrlPickerState.Deserialize
Check out this pastebin for a great sample
Jeavon
You can see there is a Count() check in there already
Hi Jeavon, thanks for your reply.
I have tried implementing the code on the pastebin example but got this error message:
I have tried adding the below as a namespace but got the same error.
Any ideas?
The using statement in my project is:
Maybe there have been different versions, but probably this is the one you need. With that in place you should be able to reference:
MultiUrlPickerState.Deserialize()
Andy
Hi John,
What version of uComponents do you have?
Jeavon
@Andy is right, it did change between versions somewhere, I think v5
I'm using uComponents v6.0.0
OK, so I have edited my script and the on save error has gone but it still says Error loading macro on the page.
Here is the first few lines of my code.
I really have no clue where it doesn't work.
Thanks again
The only issue I see is the missing @ before the using
Have you got visual studio or another IDE that can give you intellisense?
Its gonna be hard trying to write razor from Umbraco backoffice.
yea the @ is missing.
If you paste the full mark up i will put in in Visual Studio and see what resharper makes of it :).
Charlie
Thanks for your comments, all.
I have added the missing @ to no avail.
I have Visual Web Developer 2010 installed and have been using that in some cases to write the scripts, but it doesn't show any errors, and the whole site builds without errors.
Here is my full script.
Does anyone have any thoughts on this at all please?
I've solved this in the end.
I couldnt figure out how to count the items at all without it erroring, so thought of something else which probably isn't very pretty and if there a lot of related products might take a little while, but for my purposes it should be fine.
Is the MultiUrlPickerState object returning an Enum?
so links will = a collection of MultiUrlPickerStates
so you should be able to do links.count, which will give the total number of MultiUrlPickerState object in the collection. :).
Charlie
is working on a reply...