Creating a Partial View to show certain fields from product variants

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
12 yıl önce
Hi ,

I want to show certain fields from the product variant table, therfore I created a partial view for it but for some reason its not working can anyone assist me with it , below is the code which i added into following class, controller and view:
ProductModel.cs:

public class ProductModel : BaseNopEntityModel
    {
        public ProductModel()
        {
            ProductMisc = new ProductMiscModel();
}
public ProductMiscModel ProductMisc { get; set; }
public class ProductMiscModel : BaseNopModel
        {
            public string Sku { get; set; }

            public bool ShowSku { get; set; }
        }
}

CatalogController.cs:
[NonAction]
        private ProductModel.ProductMiscModel PrepareProductMiscModel(Product product)
        {
            if (product == null)
               throw new ArgumentNullException("product");

            var model = new ProductModel.ProductMiscModel();
            var productVariants = _productService.GetProductVariantsByProductId(product.Id);
          
                        var productVariant = productVariants[0];
                      
                        model.Sku = productVariant.Sku;
                        model.ShowSku = true;
                        return model;}

Created a partial view _ProductMisc.cshtml:
@model Nop.Web.Models.Catalog.ProductModel.ProductMiscModel
@Model.Sku

Finally integrated the partial view in the _ProductBox.cshtml:
<div class="description">
       @Html.Partial("_ProductMisc", Model.ProductMisc)
        @Html.Raw(Model.FullDescription) <br />
        
    </div>

But sku doesnt not comes up in the view , where am i getting it wrong???
12 yıl önce
creative3k wrote:
Hi ,

I want to show certain fields from the product variant table, therfore I created a partial view for it but for some reason its not working can anyone assist me with it , below is the code which i added into following class, controller and view:
ProductModel.cs:

public class ProductModel : BaseNopEntityModel
    {
        public ProductModel()
        {
            ProductMisc = new ProductMiscModel();
}
public ProductMiscModel ProductMisc { get; set; }
public class ProductMiscModel : BaseNopModel
        {
            public string Sku { get; set; }

            public bool ShowSku { get; set; }
        }
}

CatalogController.cs:
[NonAction]
        private ProductModel.ProductMiscModel PrepareProductMiscModel(Product product)
        {
            if (product == null)
               throw new ArgumentNullException("product");

            var model = new ProductModel.ProductMiscModel();
            var productVariants = _productService.GetProductVariantsByProductId(product.Id);
          
                        var productVariant = productVariants[0];
                      
                        model.Sku = productVariant.Sku;
                        model.ShowSku = true;
                        return model;}

Created a partial view _ProductMisc.cshtml:
@model Nop.Web.Models.Catalog.ProductModel.ProductMiscModel
@Model.Sku

Finally integrated the partial view in the _ProductBox.cshtml:
<div class="description">
       @Html.Partial("_ProductMisc", Model.ProductMisc)
        @Html.Raw(Model.FullDescription) <br />
        
    </div>

But sku doesnt not comes up in the view , where am i getting it wrong???


I would check that your prepare method is called correctly in the controller, and I would also ensure that all the product variants have SKU's. Since you're just grabbing the first record without explicit ordering it is possible the variant at ordinal zero doesn't have a SKU (maybe it is a different variant then you're expecting).

Have you tried adding HTML to the partial view to check if it is being rendered on the page?
12 yıl önce
sku is not a required field so if the products which dont have sku should not display sku and the products with sku shud display sku , yeh i check the partial view by entering some testing text , which shows up in the view which means the prepare method is returning no sku.
12 yıl önce
I even tried by hardcoding the values for sku but still nuthing comes up :

private ProductModel.ProductMiscModel PrepareProductMiscModel(Product product)
        {
            if (product == null)
               throw new ArgumentNullException("product");

            var model = new ProductModel.ProductMiscModel();
            var productVariants = _productService.GetProductVariantsByProductId(product.Id);
            switch (productVariants.Count)
            {
                case 0:
                    {
                        var productVariant = productVariants[0];
                        //no variants
                        // model.ShowSku = false;
                        model.Sku = "0"; //null;
                        model.ShowSku = true;
                        //model.Sku = null;
                    } break;



                case 1:
                    //only one variant
                    {
                                    var productVariant = productVariants[0];

                        //if (!_catalogSettings.HidePricesForNonRegistered ||
                        //    !_workContext.CurrentCustomer.IsGuest())
                        //{}
                                    model.Sku = "0"; //null;
                                    model.ShowSku = true;

                    }
                    break;

            }
            //}
            return model;
        }
12 yıl önce
Tried debugging it , its not even going to that method , why is that so ???
12 yıl önce
creative3k wrote:
I even tried by hardcoding the values for sku but still nuthing comes up :

private ProductModel.ProductMiscModel PrepareProductMiscModel(Product product)
        {
            if (product == null)
               throw new ArgumentNullException("product");

            var model = new ProductModel.ProductMiscModel();
            var productVariants = _productService.GetProductVariantsByProductId(product.Id);
            switch (productVariants.Count)
            {
                case 0:
                    {
                        var productVariant = productVariants[0];
                        //no variants
                        // model.ShowSku = false;
                        model.Sku = "0"; //null;
                        model.ShowSku = true;
                        //model.Sku = null;
                    } break;



                case 1:
                    //only one variant
                    {
                                    var productVariant = productVariants[0];

                        //if (!_catalogSettings.HidePricesForNonRegistered ||
                        //    !_workContext.CurrentCustomer.IsGuest())
                        //{}
                                    model.Sku = "0"; //null;
                                    model.ShowSku = true;

                    }
                    break;

            }
            //}
            return model;
        }


Can you provide the code where you call PrepareProductMiscModel?
12 yıl önce
skyler.severns wrote:
I even tried by hardcoding the values for sku but still nuthing comes up :

private ProductModel.ProductMiscModel PrepareProductMiscModel(Product product)
        {
            if (product == null)
               throw new ArgumentNullException("product");

            var model = new ProductModel.ProductMiscModel();
            var productVariants = _productService.GetProductVariantsByProductId(product.Id);
            switch (productVariants.Count)
            {
                case 0:
                    {
                        var productVariant = productVariants[0];
                        //no variants
                        // model.ShowSku = false;
                        model.Sku = "0"; //null;
                        model.ShowSku = true;
                        //model.Sku = null;
                    } break;



                case 1:
                    //only one variant
                    {
                                    var productVariant = productVariants[0];

                        //if (!_catalogSettings.HidePricesForNonRegistered ||
                        //    !_workContext.CurrentCustomer.IsGuest())
                        //{}
                                    model.Sku = "0"; //null;
                                    model.ShowSku = true;

                    }
                    break;

            }
            //}
            return model;
        }

Can you provide the code where you call PrepareProductMiscModel?


I didnt call the method any where , just call the partial view in the productbox
12 yıl önce
skyler.severns wrote:
I even tried by hardcoding the values for sku but still nuthing comes up :

private ProductModel.ProductMiscModel PrepareProductMiscModel(Product product)
        {
            if (product == null)
               throw new ArgumentNullException("product");

            var model = new ProductModel.ProductMiscModel();
            var productVariants = _productService.GetProductVariantsByProductId(product.Id);
            switch (productVariants.Count)
            {
                case 0:
                    {
                        var productVariant = productVariants[0];
                        //no variants
                        // model.ShowSku = false;
                        model.Sku = "0"; //null;
                        model.ShowSku = true;
                        //model.Sku = null;
                    } break;



                case 1:
                    //only one variant
                    {
                                    var productVariant = productVariants[0];

                        //if (!_catalogSettings.HidePricesForNonRegistered ||
                        //    !_workContext.CurrentCustomer.IsGuest())
                        //{}
                                    model.Sku = "0"; //null;
                                    model.ShowSku = true;

                    }
                    break;

            }
            //}
            return model;
        }

Can you provide the code where you call PrepareProductMiscModel?


Sorted out I actually didnt call that method and was expecting a miracle lol , wasted 4 hours for just one to be called in the method:
[NonAction]
        private ProductModel PrepareProductOverviewModel(Product product, bool preparePriceModel = true, bool preparePictureModel = true)
        {
model.ProductMisc = PrepareProductMiscModel(product);

///////code////

}

Anythnx for assistance , ur one word 'call' worked :)
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.