无法转换类型为异常的对象

时间:2019-01-22 00:02:12

标签: c# asp.net asp.net-mvc nopcommerce

这个问题刚刚出现,但是我还没有修改与此关联的代码。所以不太确定如何解决它。

这是短信:

  

无法转换类型的对象   '<> f__AnonymousType16 2[Nop.Web.Models.Media.PictureModel,System.Collections.Generic.List 1 [Nop.Web.Models.Media.PictureModel]]'   输入   '<> f__AnonymousType4 2[Nop.Web.Models.Media.PictureModel,System.Collections.Generic.List 1 [Nop.Web.Models.Media.PictureModel]]'。

完全例外:

  

System.InvalidCastException:无法转换类型的对象   '<> f__AnonymousType16 2[Nop.Web.Models.Media.PictureModel,System.Collections.Generic.List 1 [Nop.Web.Models.Media.PictureModel]]'   输入   '<> f__AnonymousType4 2[Nop.Web.Models.Media.PictureModel,System.Collections.Generic.List 1 [Nop.Web.Models.Media.PictureModel]]”。   在Nop.Core.Caching.MemoryCacheManager.Get [T](String key)在   Nop.Core.Caching.CacheExtensions.Get [T](ICacheManager cacheManager,   字符串键,Int32 cacheTime,Func 1 acquire) at Nop.Core.Caching.CacheExtensions.Get[T](ICacheManager cacheManager, String key, Func 1获取)   Nop.Web.Controllers.ProductController.PrepareProductDetailsPageModel(Product   product,ShoppingCartItem updatecartitem,Boolean isAssociatedProduct,   可为空的1 childProductId) at Nop.Web.Controllers.ProductController.ProductDetails(Int32 productId, String c, String s, Int32 updatecartitemid) at lambda_method(Closure , ControllerBase , Object[] ) at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary 2个参数)   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext   controllerContext,ActionDescriptor actionDescriptor,IDictionary 2 parameters) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult 2.CallEndDelegate(IAsyncResult   asyncResult)   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult   asyncResult)   System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.b__3d()   在   System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters。<> c__DisplayClass46.b__3f()   在   System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters。<> c__DisplayClass46.b__3f()   在   System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters。<> c__DisplayClass46.b__3f()   在   System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters。<> c__DisplayClass46.b__3f()   在   System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters。<> c__DisplayClass46.b__3f()   在   System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters。<> c__DisplayClass46.b__3f()   在   System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters。<> c__DisplayClass46.b__3f()   在   System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters。<> c__DisplayClass46.b__3f()   在   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult   asyncResult)   System.Web.Mvc.Async.AsyncControllerActionInvoker。<> c__DisplayClass21。<> c__DisplayClass2b.b__1c()   在   System.Web.Mvc.Async.AsyncControllerActionInvoker。<> c__DisplayClass21.b__1e(IAsyncResult   asyncResult)   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult   asyncResult)   System.Web.Mvc.Controller.b__1d(IAsyncResult   asyncResult,ExecuteCoreState innerState)位于   System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid 1.CallEndDelegate(IAsyncResult asyncResult) at System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid 1.CallEndDelegate(IAsyncResult   System.Web.Mvc.Controller.EndExecute(IAsyncResult)上的asyncResult)   asyncResult)   System.Web.Mvc.MvcHandler.b__5(IAsyncResult   asyncResult,ProcessRequestState innerState)位于   System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult   asyncResult)   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)   在   System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()   在System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep步骤)   System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤,布尔值&   同步完成)

这是异常所指向的代码:

model.DefaultPictureZoomEnabled = _mediaSettings.DefaultPictureZoomEnabled;
        //default picture
        var defaultPictureSize = isAssociatedProduct
            ? _mediaSettings.AssociatedProductPictureSize
            : _mediaSettings.ProductDetailsPictureSize;
        //prepare picture models
        var productPicturesCacheKey = string.Format(ModelCacheEventConsumer.PRODUCT_DETAILS_PICTURES_MODEL_KEY,
            product.Id, defaultPictureSize, isAssociatedProduct, _workContext.WorkingLanguage.Id,
            _webHelper.IsCurrentConnectionSecured(), _storeContext.CurrentStore.Id);
        var cachedPictures = _cacheManager.Get(productPicturesCacheKey, () =>
        {
            var pictures = _pictureService.GetPicturesByProductId(product.Id);
            var defaultPicture = pictures.FirstOrDefault();
            var defaultPictureModel = new PictureModel
            {
                ImageUrl = _pictureService.GetPictureUrl(defaultPicture, defaultPictureSize, !isAssociatedProduct),
                FullSizeImageUrl = _pictureService.GetPictureUrl(defaultPicture, 0, !isAssociatedProduct),
                Title =
                    string.Format(_localizationService.GetResource("Media.Product.ImageLinkTitleFormat.Details"),
                        model.Name),
                AlternateText =
                    string.Format(
                        _localizationService.GetResource("Media.Product.ImageAlternateTextFormat.Details"),
                        model.Name),
            };
            //"title" attribute
            defaultPictureModel.Title = (defaultPicture != null &&
                                         !string.IsNullOrEmpty(defaultPicture.TitleAttribute))
                ? defaultPicture.TitleAttribute
                : string.Format(_localizationService.GetResource("Media.Product.ImageLinkTitleFormat.Details"),
                    model.Name);
            //"alt" attribute
            defaultPictureModel.AlternateText = (defaultPicture != null &&
                                                 !string.IsNullOrEmpty(defaultPicture.AltAttribute))
                ? defaultPicture.AltAttribute
                : string.Format(_localizationService.GetResource("Media.Product.ImageAlternateTextFormat.Details"),
                    model.Name);

            //all pictures
            var pictureModels = new List<PictureModel>();
            foreach (var picture in pictures)
            {
                var pictureModel = new PictureModel
                {
                    ImageUrl =
                        _pictureService.GetPictureUrl(picture,
                            _mediaSettings.ProductThumbPictureSizeOnProductDetailsPage),
                    FullSizeImageUrl = _pictureService.GetPictureUrl(picture),
                    Title =
                        string.Format(
                            _localizationService.GetResource("Media.Product.ImageLinkTitleFormat.Details"),
                            model.Name),
                    AlternateText =
                        string.Format(
                            _localizationService.GetResource("Media.Product.ImageAlternateTextFormat.Details"),
                            model.Name),
                };
                //"title" attribute
                pictureModel.Title = !string.IsNullOrEmpty(picture.TitleAttribute)
                    ? picture.TitleAttribute
                    : string.Format(_localizationService.GetResource("Media.Product.ImageLinkTitleFormat.Details"),
                        model.Name);
                //"alt" attribute
                pictureModel.AlternateText = !string.IsNullOrEmpty(picture.AltAttribute)
                    ? picture.AltAttribute
                    : string.Format(
                        _localizationService.GetResource("Media.Product.ImageAlternateTextFormat.Details"),
                        model.Name);

                pictureModels.Add(pictureModel);
            }

            return new {DefaultPictureModel = defaultPictureModel, PictureModels = pictureModels};
        });
        model.DefaultPictureModel = cachedPictures.DefaultPictureModel;
        model.PictureModels = cachedPictures.PictureModels;

我试图像这样将图片发送到列表中,但是没有用。

var pictures = _pictureService.GetPicturesByProductId(product.Id).ToList();

任何人都知道这里发生了什么,我以前从未遇到过此类错误。 谢谢

0 个答案:

没有答案