Unable to use @model ProductModel in plugin view

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
6 years ago
Hello,

I'm using nopCommerce 3.90 with source.

I developed a new plugin using the BasePlugin, IMiscPlugin, IAdminMenuPlugin references on my plugin class.

I am having a terrible time trying to use the @model ProductModel in a view.  It will not find it and gives me all kinds of errors.

I have it at the beginning of my view like this:

@using Nop.Web
@using Nop.Admin
@using Nop.Admin.Models.Catalog
@using Nop.Web.Framework
@using Nop.Web.Framework.UI
@using Nop.Core.Domain.Catalog
@using Nop.Services
@using System.Web.Mvc
@model ProductModel


I don't think I need all those using statements, but I wanted to see if any of them would make a difference.

When I try to execute my plugin view from the Admin menu, it gives this error: object reference not set to an instance of an object on this line:
@if (Model.AddSpecificationAttributeModel.AvailableAttributes.Count > 0)
     {


I have all of these dll's in my references and more:

        Nop.Core.dll
        Nop.Data.dll
        Nop.Services.dll
        Nop.Web.Framework.dll
        EntityFramework.dll
        System.Data.Entity.dll
        System.Web.dll
        System.Web.Mvc.dll
        Autofac.dll
        Autofac.Integration.Mvc.dll


Any help anyone can provide to fix this issue would be gratefully appreciated.

Thanks,
Tony
6 years ago
Make sure that the model(instance of ProductModel) and Model.AddSpecificationAttributeModel.AvailableAttributes are not null and you added Nop.Admin at your project. As the view is strongly tied to the ProductModel so you have to provide all necessary properties values of the model.
6 years ago
Sina,

Thanks for your help.

I do have Nop.Admin as a reference in my project.

sina.islam wrote:
Make sure that the model(instance of ProductModel) and Model.AddSpecificationAttributeModel.AvailableAttributes are not null and you added Nop.Admin at your project. As the view is strongly tied to the ProductModel so you have to provide all necessary properties values of the model.


Can you explain how to do this?  Is there other code that I can look at to help me figure out what to do.

Thanks,
Tony
6 years ago
Sina,

Are you saying that I should add this:

private readonly IProductService _productService;


to the fields in  my controller?  And to put this in the parameter list of the constructor: IProductService productService

and then add this to the constructor:

this._productService = productService;


I'm trying to figure out what you mean.

Thanks,
Tony
6 years ago
You can take a look at PrepareProductModel of the ProductController of the admin. There, you will see the bellow code
and the PrepareProductModel called from the 4 different ActionResult methods.

model.AddSpecificationAttributeModel.AvailableAttributes = _cacheManager
                    .Get(ModelCacheEventConsumer.SPEC_ATTRIBUTES_MODEL_KEY, () =>
                    {
                        var availableSpecificationAttributes = new List<SelectListItem>();
                        foreach (var sa in _specificationAttributeService.GetSpecificationAttributes())
                        {
                            availableSpecificationAttributes.Add(new SelectListItem
                            {
                                Text = sa.Name,
                                Value = sa.Id.ToString()
                            });
                        }
                        return availableSpecificationAttributes;
                    });


You can see at the ProductModel of the admin where AddSpecificationAttributeModel is a property of the class.
6 years ago
Sina,

Thanks for your help.  I did get this to work with the information that you have provided.

Thanks,
Tony
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.