Getting only prevalues that are currently used in a content node
I am currently building a site that has filters for the customer to filter the products. I am getting all the prevalues, but I was wondering if there is a way to filter them, so I only display the prevalues that are actually used in the back-end. Otherwise I am displaying long lists of options, where half of them won't return any results if selected.
How many products are there? If it's a fairly small amount I would be tempted to get all products and then select this value, dedupe and order by name. Even if there's a few you could just use the code below in a cached partial to improve speed.
Also using umbraco.library.GetPreValues hits the database I think rather than using the cache so can cause issues on high load sites.
I'd suggest something like:
@{
var products = Model.Content.Children();
var dropDownValues = products.Where(x => x.HasValue("testDropDown")).Select(x => x.GetPropertyValue<string>("testDropDown")).ToList();
dropDownValues = dropDownValues.Distinct().OrderBy(x => x).ToList();
<select>
@foreach (var curValue in dropDownValues)
{
<option value="@curValue">@curValue</option>
}
</select>
}
You might need to @using System.Linq at the top of your razor script.
Thank you, that has really helped. I will have around 1500 products. I have tried your solution and it works well, but I only have 485 products loaded at the moment, so will test for speed once all are in the system.
Getting only prevalues that are currently used in a content node
I am currently building a site that has filters for the customer to filter the products. I am getting all the prevalues, but I was wondering if there is a way to filter them, so I only display the prevalues that are actually used in the back-end. Otherwise I am displaying long lists of options, where half of them won't return any results if selected.
Thank you
Hi Katie,
How many products are there? If it's a fairly small amount I would be tempted to get all products and then select this value, dedupe and order by name. Even if there's a few you could just use the code below in a cached partial to improve speed.
Also using umbraco.library.GetPreValues hits the database I think rather than using the cache so can cause issues on high load sites.
I'd suggest something like:
You might need to @using System.Linq at the top of your razor script.
HTH
Steve
Thank you, that has really helped. I will have around 1500 products. I have tried your solution and it works well, but I only have 485 products loaded at the moment, so will test for speed once all are in the system.
Thanks
In that case add it as a partial and cache it or a macro if you prefer those.
See here for details
https://our.umbraco.org/documentation/reference/templating/mvc/partial-views
is working on a reply...