Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Daniel Rogers 69 posts 364 karma points
    Apr 29, 2020 @ 09:40
    Daniel Rogers
    0

    umbraco 8 datatype prevalues getting error Unable to cast object of type 'System.Collections.Generic.Dictionary

    Not sure where to start here but I have a datatype I are trying to create. I have the user interface workingfor the page loaout side and the prevalues but are know trying to join them to gether.

    so the manifest

     editor: {
        view: "~/App_Plugins/IBD.GalleryItems/IBD.GalleryItems.View.html",
        valueType: "JSON"
      },
      prevalues : {
        fields: [
          {
            label: "Row Options",
            description: "The number of items per row and the slick slider control string",
            key: "items",
            view: "~/App_Plugins/IBD.GalleryItems/IBD.GalleryItems.Prevalues.html"
          }
        ]
      }
    

    to access request the pre values

     if ($scope.ItemsPerRow == undefined || $scope.ItemsPerRow.length == 0) {
            $scope.ItemsPerRow = [];
            $http({
                method: 'GET',
                url: '/Umbraco/Api/IBDBackOffice/GalleryItemPrevalue',
                params: { dataTypeID: 1114 }
            }).then(function (data) {
                angular.forEach(data.data,
                    function (value, key) {
                       .....process results.........
                $scope.load = true;
            }
            else {
                $scope.load = true;
            } ..........
    

    and the api routine

    [HttpGet] public HttpResponseMessage GalleryItemPrevalue(int dataTypeID) { IDataTypeService dataTypeService = Services.DataTypeService;

            var dataType = dataTypeService.GetDataType(dataTypeID);
            var cp = new List<GalleryButtonItem>();
    
            if (dataType != null)
            {
                ValueListConfiguration valueList = (ValueListConfiguration)dataType.Configuration;
    
    
                foreach (ValueListConfiguration.ValueListItem node in valueList.Items)
                {
                    GalleryButtonItem item = new GalleryButtonItem();
                    JObject json = JObject.Parse(node.Value);
    
    
                    item.Id = node.Id;
                    item.Row = json.Value<string>("Row").ToString();
                    item.Slick = json.Value<string>("Slick").ToString();
                    cp.Add(item);
                }
            }
            var resp = new HttpResponseMessage()
            {
                Content = new StringContent(cp.ToJson())
            };
            resp.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            return resp;
        }
    

    so every thing is working but it bails out here

    ValueListConfiguration valueList = (ValueListConfiguration)dataType.Configuration;
    

    this is the error

    Possibly unhandled rejection: {"data":{"Message":"An error has occurred.","ExceptionMessage":"Unable to cast object of type 'System.Collections.Generic.Dictionary2[System.String,System.Object]' to type 'Umbraco.Core.PropertyEditors.ValueListConfiguration'.","ExceptionType":"System.InvalidCastException","StackTrace":" at IBDBackOfficeApi.IBDBackOfficeController.GalleryItemPrevalue(Int32 dataTypeID) in D:\\Web Development\\Zeke Project\\ZekeCore\\Api\\IbdBackOfficeApi.cs:line 26\r\n at lambda_method(Closure , Object , Object[] )\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass6_2.<GetExecutor>b__2(Object instance, Object[] methodParameters)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary2 arguments, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ApiControllerActionInvoker.

    Trying to learn something new so if anyone can get me heading in the right direction would be good.

  • Daniel Rogers 69 posts 364 karma points
    Apr 29, 2020 @ 14:29
    Daniel Rogers
    100

    Found a solution but there has to be a better way.

            if (dataType != null)
            {
                var propEditor = Current.PropertyEditors.Where(x => x.Alias == dataType.EditorAlias).FirstOrDefault();
                var valueList = propEditor.GetConfigurationEditor().ToValueEditor(dataType.Configuration);
    
                foreach (var node in valueList.Values)
                {
                    JObject json = JObject.Parse(node.ToString());
                    foreach (var items in json.Values())
                    {
                        foreach (var bttn in items)
                        {
                            GalleryButtonItem item = new GalleryButtonItem();
    
                            item.Row = bttn.Value<string>("Rows").ToString();
                            item.Slick = bttn.Value<string>("Slick").ToString();
                            cp.Add(item);
                        }
                    }
                }
            }
            var resp = new HttpResponseMessage()
            {
                Content = new StringContent(cp.ToJson())
            };
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies