Unable to cast object of type 'nuPickers.Picker[]' to type 'System.Collections.Generic.IEnumerable`1[Client.Web.Models.FeatureFacility]'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Unable to cast object of type 'nuPickers.Picker[]' to type 'System.Collections.Generic.IEnumerable`1[Client.Web.Models.FeatureFacility]'.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[InvalidCastException: Unable to cast object of type 'nuPickers.Picker[]' to type 'System.Collections.Generic.IEnumerable`1[Client.Web.Models.FeatureFacility]'.]
lambda_method(Closure , Object , Object ) +99
Our.Umbraco.Ditto.PropertyInfoInvocations.SetValue(PropertyInfo property, Object instance, Object value) +219
Our.Umbraco.Ditto.PublishedContentExtensions.ConvertContent(IPublishedContent content, Type type, CultureInfo culture, Object instance, IEnumerable`1 processorContexts, Action`1 onConverting, Action`1 onConverted) +2580
Our.Umbraco.Ditto.PublishedContentExtensions.As(IPublishedContent content, Type type, CultureInfo culture, Object instance, IEnumerable`1 processorContexts, Action`1 onConverting, Action`1 onConverted) +706
Our.Umbraco.Ditto.PublishedContentExtensions.As(IPublishedContent content, CultureInfo culture, T instance, IEnumerable`1 processorContexts, Action`1 onConverting, Action`1 onConverted) +110
Client.Web.Controllers.BaseSurfaceController`1.Index(RenderModel model) +202
lambda_method(Closure , ControllerBase , Object[] ) +94
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +229
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +90
System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +39
System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +79
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +42
System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +72
System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +386
System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +386
System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +386
System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +386
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +42
System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +38
System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +186
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +38
System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +29
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +67
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +35
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +38
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +44
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +67
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +38
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +674
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +187
The property in the page's model:
public IEnumerable<FeatureFacility> Facilities { get; set; }
The class for the FeatureFacility:
[NuPicker]
public class FeatureFacility : PublishedContentModel
{
public FeatureFacility(IPublishedContent content) : base(content)
{
}
//[DittoIgnore]
//public string ItemName => Content.GetSafeVortoString("itemName", Content.Name);
}
(At the moment, I have everything commented out because I wanted to make sure it wasn't my properties causing the problem)
The NuPicker attribute (copied directly from James's link at the top, which again, I've used before but not in 7.6.3...):
/// <summary>
/// Provides a unified way of converting the <see cref="Picker"/> type to the given <see cref="Type"/>
/// </summary>
public class NuPickerAttribute : DittoMultiProcessorAttribute
{
/// <summary>
/// Initializes a new instance of the <see cref="NuPickerAttribute"/> class.
/// </summary>
public NuPickerAttribute()
: base(new DittoProcessorAttribute[] { new NuPickerConverterAttribute(), new UmbracoPickerAttribute() })
{
}
/// <summary>
/// Returns the correct value to pass to the <see cref="UmbracoPickerAttribute"/>
/// </summary>
private class NuPickerConverterAttribute : DittoProcessorAttribute
{
/// <inheritdoc/>
public override object ProcessValue()
{
Picker picker = this.Value as Picker;
if (picker != null && picker.PickedKeys.Any())
{
return picker.PickedKeys;
}
return this.Value;
}
}
}
public IEnumerable<FeatureFacility> Facilities { get; set; }
I note that you appear to have no attributes decorating it, which I would expect to be a problem, as Ditto will not know how to convert your Ditto objects to FeatureFacility instances. Maybe it should be decorated with [NuPickerConverter]?
Also, even if you use that converter, it seems to return something specific to nuPickers. Wouldn't you also need something to convert that to instances of FeatureFacility? Maybe the DittoFactory or something modeled after that would be of use here? https://github.com/leekelleher/umbraco-ditto/issues/168
Or maybe not. I'm not too clear on how you're converting the picked items to the facility instances.
Hi Nick, thanks for the feedback :) I have the converter (although they're called processors now, I believe) attribute decorating the FeatureFacility class, which has always worked in the past. I have tried taking it off the class and decorating just the property instead and that gave me the same error (but it was worth a shot!).
The value that NuPickers returns is a content ID, which then converts to IPublishedContent. Historically, Ditto has known to to convert that into a model based on the IPublishedContent (or, at least, it always has in previous projects I've worked on... just not this one).
I have the converter (although they're called processors now, I believe) attribute decorating the FeatureFacility class, which has always worked in the past.
[UmbracoPicker(Order = 0)]
[NuPicker(Order = 1)]
public class FeatureFacility : PublishedContentModel
But I still have the same problem.
On my previous projects, the attributes have been set up at the class level and they worked fine. Although it's also fine on the property level. I could use them interchangeably and they worked. This code (barring the fact that this model is currently empty because I'm testing) is exactly the same as my last project, which works. >_<
As far as I can tell, something changed between 7.5.13 (the last project I used this on, the exact code) to 7.6.3 and I'm not sure what it is...
As far as I can tell, something changed between 7.5.13 (the last project I used this on, the exact code) to 7.6.3 and I'm not sure what it is...
Could it be the new "version 2" pickers that work with GUID's rather than integers? Haven't tried 7.6.x yet, but I've been reading about a few people running into that issue.
Also, you might want to create a processor like this:
using Our.Umbraco.Ditto;
using System;
[AttributeUsage(validOn:AttributeTargets.Property, AllowMultiple = true)]
public class InspectValueAttribute : UmbracoPropertyAttribute
{
public override object ProcessValue()
{
// Set a breakpoint here.
return this.Value;
}
}
Perhaps also allowing on classes rather than just properties. I use this to be able to set a breakpoint before/after processors to see what data they're inputting/outputting to narrow things down.
I'll see if I can try that processor and if it will give me anything :) I tried to step through with a breakpoint in the current NuPicker processor I have (attempted both on the property & on the class) and it won't even hit the breakpoint :(
The value that nuPickers is returning in the umbraco.config file, at least, is an integer. But I'm not sure what nuPickers itself is returning in as this.Value since I haven't been able to hit it...
I tried to step through with a breakpoint in the current NuPicker processor I have (attempted both on the property & on the class) and it won't even hit the breakpoint
FYI, that suggests to me that Ditto is ignoring your class-level processor and is instead running the default UmbracoProperty processor. I wonder if a breakpoint would be hit if you moved the processor to the property level.
That did resolve the problem, and I actually had it set up that way originally (it just got pulled off somewhere down the line when I was testing adding and removing anything to get it to hit my breakpoint, haha), but on the class and not the property, which didn't work. It does work on my other project when on the class, so I'm really not sure what the differences is.
But if it works when it's on the property, then I'll take it! :)
It shows that at one point I'm mapping from a collection of content to a collection of POCO classes. The "content" in my case is Archetype fieldsets, but it should be basically the same if you are mapping from a collection of IPublishedContent (though, I'm not sure if you are). I am also doing a few other things that you can ignore (e.g., getting the first item from the collection).
Unable to cast object of type nuPickers.Picker[]
(Always helps when you post with the right account and therefore the right e-mail address to follow the thread... 😫)
To be honest, I'm not sure if this is a Ditto issue or a NuPickers issue or what, but I'm trying here first!
So, I've been using the code here on multiple projects with NuPickers (although this is my first using it on 7.6.3) https://github.com/leekelleher/umbraco-ditto/issues/175#issuecomment-264146559. With the site I'm setting up now, I'm getting the following error:
The property in the page's model:
The class for the
FeatureFacility
:(At the moment, I have everything commented out because I wanted to make sure it wasn't my properties causing the problem)
The NuPicker attribute (copied directly from James's link at the top, which again, I've used before but not in 7.6.3...):
Any insights? 🤔
On this line:
I note that you appear to have no attributes decorating it, which I would expect to be a problem, as Ditto will not know how to convert your Ditto objects to
FeatureFacility
instances. Maybe it should be decorated with[NuPickerConverter]
?Also, even if you use that converter, it seems to return something specific to nuPickers. Wouldn't you also need something to convert that to instances of
FeatureFacility
? Maybe theDittoFactory
or something modeled after that would be of use here? https://github.com/leekelleher/umbraco-ditto/issues/168Or maybe not. I'm not too clear on how you're converting the picked items to the facility instances.
Hi Nick, thanks for the feedback :) I have the converter (although they're called processors now, I believe) attribute decorating the
FeatureFacility
class, which has always worked in the past. I have tried taking it off the class and decorating just the property instead and that gave me the same error (but it was worth a shot!).The value that NuPickers returns is a content ID, which then converts to IPublishedContent. Historically, Ditto has known to to convert that into a model based on the IPublishedContent (or, at least, it always has in previous projects I've worked on... just not this one).
I have not had luck with class-level processors (though I may have been using them wrong): https://our.umbraco.org/projects/developer-tools/ditto/ditto-feedback/82225-does-ditto-support-class-level-processors
Ah, yeah, I've also tried setting it up with the chain (based on this thread https://github.com/leekelleher/umbraco-ditto/issues/175) using
But I still have the same problem.
On my previous projects, the attributes have been set up at the class level and they worked fine. Although it's also fine on the property level. I could use them interchangeably and they worked. This code (barring the fact that this model is currently empty because I'm testing) is exactly the same as my last project, which works. >_<
As far as I can tell, something changed between 7.5.13 (the last project I used this on, the exact code) to 7.6.3 and I'm not sure what it is...
Could it be the new "version 2" pickers that work with GUID's rather than integers? Haven't tried 7.6.x yet, but I've been reading about a few people running into that issue.
Also, you might want to create a processor like this:
Perhaps also allowing on classes rather than just properties. I use this to be able to set a breakpoint before/after processors to see what data they're inputting/outputting to narrow things down.
I'll see if I can try that processor and if it will give me anything :) I tried to step through with a breakpoint in the current NuPicker processor I have (attempted both on the property & on the class) and it won't even hit the breakpoint :(
The value that nuPickers is returning in the umbraco.config file, at least, is an integer. But I'm not sure what nuPickers itself is returning in as
this.Value
since I haven't been able to hit it...FYI, that suggests to me that Ditto is ignoring your class-level processor and is instead running the default
UmbracoProperty
processor. I wonder if a breakpoint would be hit if you moved the processor to the property level.I'll give it a shot and let you know! :)
Hi Janae,
I think it may be missing a reference to the
UmbracoProperty
processor - so Ditto can get the value from the property first.e.g.
Hopefully that will work?
Cheers,
- Lee
Hi Lee - thanks for responding!
That did resolve the problem, and I actually had it set up that way originally (it just got pulled off somewhere down the line when I was testing adding and removing anything to get it to hit my breakpoint, haha), but on the class and not the property, which didn't work. It does work on my other project when on the class, so I'm really not sure what the differences is.
But if it works when it's on the property, then I'll take it! :)
Thanks to you and Nick for being so helpful!
FYI, this thread has some code that is somewhat related: https://our.umbraco.org/projects/developer-tools/ditto/ditto-feedback/78780-multiple-processor-chaining-issues
It shows that at one point I'm mapping from a collection of content to a collection of POCO classes. The "content" in my case is Archetype fieldsets, but it should be basically the same if you are mapping from a collection of IPublishedContent (though, I'm not sure if you are). I am also doing a few other things that you can ignore (e.g., getting the first item from the collection).
FYI,
DittoDocTypeFactory
inherits fromDittoFactory
: https://github.com/leekelleher/umbraco-ditto/blob/fa076612e6bcd421feb6fc2deb23e7fcf2e212e0/src/Our.Umbraco.Ditto/ComponentModel/Processors/DittoDocTypeFactoryAttribute.csis working on a reply...