Close

Nop Ajax Filters Not Working show a pop up box with message :-Loading the page failed

cyberspy
10 years ago
#4061 Quote
Avatar
  • 9
Boyko,

Thank you for responding so quickly.

What I am trying to achieve is this.

When a product has more than one image, I want to be able to choose which image appears on the site in the different places these images are displayed.

So, for example, the first image would appear in the carousel (I can't change the carousel, so the first, default image will appear there)  but in a product panel on the home page (I'm using your Alfresco theme btw) I want another image.

So, to achieve this, I think the simplest way is to use the display order which can be applied to product images. This gives administrators the ability to choose which image appears in each location (although the display number probably needs to be hard coded in the places where this feature is used).

This can be done by passing the display order number to the controller from the view that will display the images

So, for example, on the home page, we have the line:

        @Html.Action("HomepageProducts", "Catalog")

This is the default, and will display the first image.

I want to use this instead:

        @Html.Action("HomepageProducts", "Catalog", new { productPictureNumber = 2 })

The controller action then looks like this:

  protected IEnumerable<ProductOverviewModel> PrepareProductOverviewModels(IEnumerable<Product> products,
            bool preparePriceModel = true, bool preparePictureModel = true,
            int? productThumbPictureSize = null, bool prepareSpecificationAttributes = false,
            bool forceRedirectionAfterAddingToCart = false, int productPictureNumber = 0)
        { ... }

This number is passed to the PictureService.GetPicturesByProductId method which will use it to select the correct image (\WebProject\Libraries\Nop.Services\Media\PictureService.cs line 688)

This should be a simple change to make and would solve my problem.

This additional value would not affect the carousel - it would still get its images.

I don't want to have to create an entirely new action in the catalog controller, and then go through the app and call it whenever I want to use this feature - that's extra work, and would entail maintaining two copies of the action in the controller. One of the major features of modern code like MVC is code reuse, and having to duplicate a method to add an additional OPTIONAL parameter breaks this concept.

So, How do I work around this restriction?

Also, out of curiosity, how do you even do this, force a method to keep the same signature? All the other places that call this controller action are quite happy with it - they do not break at run time, so this 'feature' must have been explicitly encoded. How is this done?

I've got a bit of a deadline to work to here, so I'd really appreciate a quick solution to this.

Thanks & Regards

Adam
Boyko
10 years ago
#4068 Quote
Avatar
  • 1570
cyberspy wrote:
Also, out of curiosity, how do you even do this, force a method to keep the same signature? All the other places that call this controller action are quite happy with it - they do not break at run time, so this 'feature' must have been explicitly encoded. How is this done?

Thanks & Regards

Adam


Hi Adam,

Thank you for explaining what you are trying to achieve!
Please note that you can change whatever method in nopCommerce without any problems BUT not all changes are good to the plugins as some of the changes can break them. Basically some of the controllers of our plugins i.e JCarouselController, inherit from the nopCommerce CatalogController. We do this as we reuse the building of the product overview models. So when we compile our plugins they know exactly what the signature of the protected PrepareProductOverviewModels method of the Catalog controller is. Unfortunately adding an optional parameter to a method is a breaking change since it will require recompiling of all the dlls (plugins) that uses it. If we were to use a custom method for preparing the models and we didn't use the PrepareProductOverviewModels method then you wouldn't be able to change how the model is built at all. So now you can change the logic in this method of how the model is built and the same logic will be used everywhere in the store and the plugins. This I think is the right way to be done. You also have all the MVC extensibility points like ActionFilters etc.
I think the best way to approach this without writing too much code is to have both images in the PictureModel. This model has a dictionary CustomProperties that you can use to store the second image and then in the Razor views you can get the second image from the CustomProperties where necessary.

Hope this helps!

Best Regards,
Nop-Templates.com Support Team
Regards,
Nop-Templates.com Team
cyberspy
10 years ago
#4069 Quote
Avatar
  • 9
Boyko wrote:
I think the best way to approach this without writing too much code is to have both images in the PictureModel. This model has a dictionary CustomProperties that you can use to store the second image and then in the Razor views you can get the second image from the CustomProperties where necessary.


Boyko,

I understand now why the signature is so important - I didn't realise you where overriding this method.

You idea does have merit, but I still have some questions:

First, the PictureModel does not have a CustomProperties property. The definition of this model is:

namespace Nop.Web.Models.Media
{
    public partial class PictureModel : BaseNopModel
    {
        public string ImageUrl { get; set; }
        public string FullSizeImageUrl { get; set; }
        public string Title { get; set; }
        public string AlternateText { get; set; }
    }
}

Would adding an additional property to this model cause additional problems with nopTemplates plugins?
The easiest and most flexible would probably to provide an AlternateImage property, which would itself be a nested PictureModel, so all the properties are available.


Second, although just providing the second image as the alternate may be sufficient now, I'd still like to be able to select at run time (in the view) which image to provide as the alternate image. This is what the extra property was for.

However, even as I write this message I think I've solved it.
If each picture model contains a reference to a child picture model, then I can chain multiple picture models together, so all the images are returned in display order. If the PictureModel has some code to return a particular image (or the last image if the number requested is greater than the number of images available) then any view can use any image for the selected product.

I'd like your thoughts on this and especially whether or not any plugins will have similar issues when I extend the PictureModel.

Regards

Adam
Boyko
10 years ago
#4070 Quote
Avatar
  • 1570
cyberspy wrote:

First, the PictureModel does not have a CustomProperties property. The definition of this model is:

namespace Nop.Web.Models.Media
{
    public partial class PictureModel : BaseNopModel
    {
        public string ImageUrl { get; set; }
        public string FullSizeImageUrl { get; set; }
        public string Title { get; set; }
        public string AlternateText { get; set; }
    }
}



This property comes from the BaseNopModel.

cyberspy wrote:

Would adding an additional property to this model cause additional problems with nopTemplates plugins?


Adding new properties is not a breaking change and would not require recompilation of the plugins.

cyberspy wrote:

Second, although just providing the second image as the alternate may be sufficient now, I'd still like to be able to select at run time (in the view) which image to provide as the alternate image. This is what the extra property was for.

However, even as I write this message I think I've solved it.
If each picture model contains a reference to a child picture model, then I can chain multiple picture models together, so all the images are returned in display order. If the PictureModel has some code to return a particular image (or the last image if the number requested is greater than the number of images available) then any view can use any image for the selected product.

I'd like your thoughts on this and especially whether or not any plugins will have similar issues when I extend the PictureModel.


Adding new properties to the model would not break the plugins as the plugins use the existing properties and they wouldn't even know about the new one. But you can still use any newly added properties in the Razor view files (.cshtml) as they are compiled at runtime.

Hope this helps!

Best Regards,
Boyko Bonev
Nop-Templates.com Team
Regards,
Nop-Templates.com Team
cyberspy
10 years ago
#4071 Quote
Avatar
  • 9
Boyko,

Thanks!

Adam