Calling Manufacture Model within product model

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

Is it possible to call manufacture model within product model, I want to show manufacture image for each product , thats why i was thinking to include manufacture model within product model so that i can use intellisense for manufacture properties in the productbox.cshtml,

Is there any other way of achieving it or any ideas
12 years ago
creative3k wrote:
Hi ,

Is it possible to call manufacture model within product model, I want to show manufacture image for each product , thats why i was thinking to include manufacture model within product model so that i can use intellisense for manufacture properties in the productbox.cshtml,

Is there any other way of achieving it or any ideas


Hi creative3k,

It is possible to add a property for manufacturer model on the product model. If you have the source code then you will need to do the following.

1. Add a new property for manufacture(s)
2. Inside of the controller you will need to map the manufacturer domain object to the manufacturer model.
3. Update the ProductBox view to display according to your design.

I'm not at my normal computer so I do not have source code and I cannot provide code samples, but this should help get the ball rolling.
12 years ago
skyler.severns wrote:
Hi ,

Is it possible to call manufacture model within product model, I want to show manufacture image for each product , thats why i was thinking to include manufacture model within product model so that i can use intellisense for manufacture properties in the productbox.cshtml,

Is there any other way of achieving it or any ideas

Hi creative3k,

It is possible to add a property for manufacturer model on the product model. If you have the source code then you will need to do the following.

1. Add a new property for manufacture(s)
2. Inside of the controller you will need to map the manufacturer domain object to the manufacturer model.
3. Update the ProductBox view to display according to your design.

I'm not at my normal computer so I do not have source code and I cannot provide code samples, but this should help get the ball rolling.


Well I created a separate partial view named manufactureFields.cshtml
@model Nop.Web.Models.Catalog.ProductModel.ProductMiscModel
<text>Manufacture Name:</text> @Model.manufactureName

And did some modification in productmodel.cs:
public class ProductModel : BaseNopEntityModel
    {
        public ProductModel()
        {
            
            ProductMisc = new ProductMiscModel();
}
public ProductMiscModel ProductMisc { get; set; }

        public class ProductMiscModel : BaseNopModel
        {
     public string manufactureName { get; set; }

           }

And in catalogController:
private ProductModel.ProductMiscModel PrepareProductMiscModel(Product product)
        {
          var manufacturer = _manufacturerService.GetProductManufacturersByProductId(product.Id, true);
             var manufactureid=0;
            foreach (var manid in manufacturer)
             {
                 manufactureid = manid.ManufacturerId;
             }
             var getmanufacture = _manufacturerService.GetManufacturerById(manufactureid);
             var manufacturePicture = getmanufacture.PictureId;
             if (getmanufacture.Name == "")
             {
                 model.manufactureName = "No Manufacture Provided";
             }
             else
             {
                 model.manufactureName = getmanufacture.Name;
             }

but this gives an error :System.NullReferenceException: Object reference not set to an instance of an object.
12 years ago
idk about nopCommerce that much, but generally Object ref exceptions are caused by calling something that isn't there.

Easiest way to diagnose, is to look at the stack trace, and see where the error is originating, perhaps the "PictureID" property isn't set yet etc.

Putting a couple breakpoints in and seeing where it blows up is also a good way to track down the source of the error. I expect your missing a call somewhere and one of your objects isn't fully populated.
12 years ago
Evil Dev wrote:
idk about nopCommerce that much, but generally Object ref exceptions are caused by calling something that isn't there.

Easiest way to diagnose, is to look at the stack trace, and see where the error is originating, perhaps the "PictureID" property isn't set yet etc.

Putting a couple breakpoints in and seeing where it blows up is also a good way to track down the source of the error. I expect your missing a call somewhere and one of your objects isn't fully populated.


Yep i debugged it and found out it was returning null i modified the code in the catalogcontroller:
var manufactureDetails = _manufacturerService.GetProductManufacturersByProductId(product.Id)
               .Select(x =>
               {
                   var m = x.Manufacturer.ToModel();

                   m.PictureModel.ImageUrl = _pictureService.GetPictureUrl(x.Manufacturer.PictureId, _mediaSetting.ManufacturerThumbPictureSize, true);
                   m.PictureModel.Title = string.Format(_localizationService.GetResource("Media.Manufacturer.ImageLinkTitleFormat"), m.Name);
                   m.PictureModel.AlternateText = string.Format(_localizationService.GetResource("Media.Manufacturer.ImageAlternateTextFormat"), m.Name);
                   return m;
               })
               .ToList();
            model.manufactureName = manufactureDetails ;

And called that in my view :

@model Nop.Web.Models.Catalog.ProductModel.ProductMiscModel

<div style="float:right"> @foreach (var Manufacture in Model.manufactureName)
                               {
                                 <img style="border-width: 0px;" alt="@Manufacture.PictureModel.AlternateText" src="@Manufacture.PictureModel.ImageUrl" title="@Manufacture.PictureModel.Title" />
                                   // @test.PictureModel.ImageUrl}
                               }
                               </div>

Its working perfectly now , I can display manufacture image , details of each product within the prodect model.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.