I am struggeling with the following issue.
In my cshtml file i have the following code:
var uniqueMateriaal = Model.Children.Where(where).DescendantsOrSelf("Product").Select(x => x.GetPropertyValue("materiaal")).Distinct().OrderBy(z => z);
Now I would like to move this line within a UmbracoApiController, however this does not recognize the model.
I allready tried to retrieve the same by accessing the page first with
Umbraco.Content(nodeId);
In this case the lambda expressions won't get accepted.
Do I have to cast it or is there another solution?
I am using the where-statement to filter the collection. I've stripped the code to specify it some more:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Umbraco.Web.WebApi;
using Umbraco;
namespace Dts.Controllers
{
public class ZoekController : UmbracoApiController
{
[System.Web.Http.HttpGet]
public string getResult(int nodeId, string kleurgroep, string materiaal, string vorm, string formaat)
{
string returnValue = "test";
string where = "Visible";
var uniqueMateriaal = Umbraco.TypedContent(nodeId).Children.Where(where).DescendantsOrSelf("Product").Select(x => x.GetPropertyValue("materiaal")).Distinct().OrderBy(z => z);
return returnValue;
}
}
}
So I want to filter the uniqeMateriaal collection to be at least visible and in the final version I will add some more filters to the where-string throught the parameters.
However your hint did'nt help, I hope this makes it a little more clear.
It's a first attempt at this kind of sollution through a controller, so forgive me if I've got the wrong starting point.
I am afraid it does not compile.
It trips over x.IsVisible() and gives the following error:
'Umbraco.Core.Models.IPublishedContent' does not contain a definition for 'IsVisible' and no extension method 'IsVisible' accepting a first argument of type 'Umbraco.Core.Models.IPublishedContent' could be found (are you missing a using directive or an assembly reference?)
Ah that's it, thanks Sebastaan!
Only one slight problem left, x => x.GetPropertyValue("materiaal") doesn't seem to work anymore. If I change it in x => x.Name it still works. Any suggestions?
Sorry should've been a little more specific.
My code is now as follows:
string where = "Visible";
var products = Umbraco.TypedContent(nodeId).DescendantsOrSelf("Product").Where(where);
var materials = products.Select(x => x.GetPropertyValue<string>("materiaal").ToLowerInvariant());
var uniqueMaterials = materials.Distinct().OrderBy(z => z);
However it breaks after declaring materials. The result view in my (VS) watch responds as a results view: 'Children could not be evaluated'. If I would change x => x.GetPropertyValue<string>("materiaal").ToLowerInvariant() into: x => x.Name I do get a result. However not the result I would like to see ;)
That exact code works on my machine. Are you sure that the document type Product has a property with the alias materiaal on it? And than there's nodes under your nodeId of document type Product?
I see now that you're only selecting the materials, might be best not to do the .ToLowerInvariant() to preserve the original casing.
The strange thins is, when I use, almost, the same code in my razor file, like:
var uniqueMateriaal = Model.Children.Where(where).DescendantsOrSelf("Product").Select(x => x.GetPropertyValue("materiaal")).Distinct().OrderBy(z => z);
It does return the result as needed.
Only difference is I use Model.Children as my starting point, however I can't seem to get it to work with the same syntax.
There is nothing wrong with some old-fashioned checking.
Result is, your code works, which is the most important.
Allthough the question which remains is why:
Probably because you're asking for .Children first and then for the descendants of those children, so there's probably no descendants with document type "Product" (because the children ARE the products). So if you remove the .Children in the second query it should work, also note that you need to move the Where too:
UmbracoApiController Model alternative
I am struggeling with the following issue. In my cshtml file i have the following code:
Now I would like to move this line within a UmbracoApiController, however this does not recognize the model. I allready tried to retrieve the same by accessing the page first with
In this case the lambda expressions won't get accepted. Do I have to cast it or is there another solution?
Umbraco.Content
is of typedynamic
so you get no lambda's there. I think what you're looking for isUmbraco.TypedContent
Hi Sebastiaan,
Thanks for responding, but when I change Model with
Umbraco.TypedContent(nodeId)
I cannot perform a where filter on it's children.Hi Patrick,
Do you have an example of your lambda expressions? It should still work, so there might just be a tiny thing that you need to change.
Not sure exactly what you're doing or what your
where
variable is, but I was hinting at the solution being something like this:I am using the where-statement to filter the collection. I've stripped the code to specify it some more:
So I want to filter the uniqeMateriaal collection to be at least visible and in the final version I will add some more filters to the where-string throught the parameters. However your hint did'nt help, I hope this makes it a little more clear. It's a first attempt at this kind of sollution through a controller, so forgive me if I've got the wrong starting point.
It should compile if you change the line to:
I am afraid it does not compile. It trips over
x.IsVisible()
and gives the following error:'Umbraco.Core.Models.IPublishedContent' does not contain a definition for 'IsVisible' and no extension method 'IsVisible' accepting a first argument of type 'Umbraco.Core.Models.IPublishedContent' could be found (are you missing a using directive or an assembly reference?)
Make sure to have the following usings in your class:
Especially
Umbraco.Web
is important as that's where theIsVisible
extension method lives.Tip: Try ReSharper out, it gives you hints about missing usings. :)
Ah that's it, thanks Sebastaan! Only one slight problem left,
x => x.GetPropertyValue("materiaal")
doesn't seem to work anymore. If I change it inx => x.Name
it still works. Any suggestions?Not sure what you mean? How does it not work?
Maybe try to change it to
x => x.GetPropertyValue<string>("materiaal")
?Or just break it up into smaller pieces to see what goes wrong and to inspect the collection you're getting.
etc.. maybe you even need to make sure materials all have the same casing:
x => x.GetPropertyValue<string>("materiaal").ToLowerInvariant()
Sorry should've been a little more specific. My code is now as follows:
However it breaks after declaring materials. The result view in my (VS) watch responds as a results view: 'Children could not be evaluated'. If I would change
x => x.GetPropertyValue<string>("materiaal").ToLowerInvariant()
into:x => x.Name
I do get a result. However not the result I would like to see ;)That exact code works on my machine. Are you sure that the document type
Product
has a property with the aliasmateriaal
on it? And than there's nodes under yournodeId
of document typeProduct
?I see now that you're only selecting the materials, might be best not to do the
.ToLowerInvariant()
to preserve the original casing.Yep,
The strange thins is, when I use, almost, the same code in my razor file, like:
It does return the result as needed. Only difference is I use Model.Children as my starting point, however I can't seem to get it to work with the same syntax.
That works too if I change "Visible" to
x => x.IsVisible()
:Okay well next up.. can you guess? Ah good-old-fashioned for-each:
What do you get for
materials.Count();
if you do this?There is nothing wrong with some old-fashioned checking. Result is, your code works, which is the most important. Allthough the question which remains is why:
works, and why the following doesn not:
Which seems to be caused by the addition of DescendantsOrSelf
Probably because you're asking for
.Children
first and then for the descendants of those children, so there's probably no descendants with document type "Product" (because the children ARE the products). So if you remove the.Children
in the second query it should work, also note that you need to move the Where too:Sebastiaan thanks for your help and hints!
is working on a reply...