在 nopcommerce 中使用部分视图时出错

发布于 2024-12-16 16:08:33 字数 5567 浏览 1 评论 0原文

我正在使用 nopcommerce 2.2 和尝试在 _Productbox.cshtml 中使用部分视图 ProductVariant_SKU_Man_Stock 但出现错误: 传入字典的模型项的类型为

'System.Collections.Generic.List`1[Nop.Web.Models.Catalog.ProductModel+ProductVariantModel]', 但这本字典需要一个类型的模型项 'Nop.Web.Models.Catalog.ProductModel+ProductVariantModel'。

下面是 _Productbox.cshtml

@model Nop.Web.Models.Catalog.ProductModel
<div class="product-item">
<h2 class="product-title">
@Model.Name
</h2>
<div class="description">
      @Html.Partial("_ProductVariant_SKU_Man_Stock",Model.ProductVariantModels)
    @Html.Raw(Model.FullDescription) <br />
    @Model.Size
</div>
<div class="add-info">
    <div class="prices">
        @Html.Partial("_ProductPrice", Model.ProductPrice)
    </div>
    <div class="buttons">
       @* <input type="button" value="@T("Products.Details")" class="productlistproductdetailbutton" onclick="setLocation('@Url.RouteUrl("Product", new { productId = Model.Id, SeName = Model.SeName })')" />
*@        @if (!Model.ProductPrice.DisableBuyButton)
        {
            <br />
            <input type="button" value="@T("ShoppingCart.AddToCart")" class="productlistaddtocartbutton" onclick="setLocation('@(@Url.RouteUrl("AddProductToCart", new { productId = Model.Id }))')" />
        }
     </div>
     </div>
     </div>

有没有其他方法可以做到这一点,因为我需要该部分视图中的一些字段才能在此视图中显示。

Productmodel.cs:

 public class ProductModel : BaseNopEntityModel
{
    public ProductModel()
    {
        ProductPrice = new ProductPriceModel();
       // ProductSku = new ProductSkuModel();
        DefaultPictureModel = new PictureModel();
        PictureModels = new List<PictureModel>();
        ProductVariantModels = new List<ProductVariantModel>();
        SpecificationAttributeModels = new List<ProductSpecificationModel>();
    }

    public string Name { get; set; }

    public string ShortDescription { get; set; }

    public string FullDescription { get; set; }

    public string ProductTemplateViewPath { get; set; }

    public string MetaKeywords { get; set; }

    public string MetaDescription { get; set; }

    public string MetaTitle { get; set; }

    public string SeName { get; set; }
    public string Size { get; set; }
    public ProductPriceModel ProductPrice { get; set; }
    public bool DefaultPictureZoomEnabled { get; set; }
    public PictureModel DefaultPictureModel { get; set; }
    public IList<PictureModel> PictureModels { get; set; }
    public IList<ProductVariantModel> ProductVariantModels { get; set; }
    public IList<ProductSpecificationModel> SpecificationAttributeModels { get; set; }
    public class ProductVariantModel : BaseNopEntityModel
    {
        public ProductVariantModel()
        {
            ProductSku = new ProductSkuModel();
            GiftCard = new GiftCardModel();
            ProductVariantPrice = new ProductVariantPriceModel();
            PictureModel = new PictureModel();
            AddToCart = new AddToCartModel();
            ProductVariantAttributes = new List<ProductVariantAttributeModel>();
        }

        public string Name { get; set; }

        public bool ShowSku { get; set; }

        public string Sku { get; set; }

        public string Description { get; set; }

        public bool ShowManufacturerPartNumber { get; set; }

        public string ManufacturerPartNumber { get; set; }

        public string DownloadSampleUrl { get; set; }

        public GiftCardModel GiftCard { get; set; }

        public string StockAvailablity { get; set; }

        public ProductVariantPriceModel ProductVariantPrice { get; set; }

        public AddToCartModel AddToCart { get; set; }

        public PictureModel PictureModel { get; set; }
        public ProductSkuModel ProductSku { get; set; }

        public IList<ProductVariantAttributeModel> ProductVariantAttributes { get; set; }
        public class ProductVariantPriceModel : BaseNopModel
        {
            public string OldPrice { get; set; }

            public string Price { get; set; }
            public string PriceWithDiscount { get; set; }

            public decimal PriceValue { get; set; }
            public decimal PriceWithDiscountValue { get; set; }

            public bool CustomerEntersPrice { get; set; }

            public bool CallForPrice { get; set; }

            public int ProductVariantId { get; set; }

            public bool HidePrices { get; set; }

            public bool DynamicPriceUpdate { get; set; }
        }
        public class ProductSkuModel : BaseNopModel
        {


            public string Sku { get; set; }

            public bool ShowSku { get; set; }
        }

想要使用此部分视图:

@model Nop.Web.Models.Catalog.ProductModel.ProductVariantModel
@if (!String.IsNullOrWhiteSpace(Model.StockAvailablity))
{
    <div class="stock">

        @Model.StockAvailablity
    </div>
 }
<div class="clear">
</div>
@if (!String.IsNullOrWhiteSpace(Model.Sku) && Model.ShowSku)
{
    <div class="sku">

        @Model.Sku
    </div>
}
<div class="clear">
</div>
@if (!String.IsNullOrWhiteSpace(Model.ManufacturerPartNumber) && Model.ShowManufacturerPartNumber)
{
    <div class="manufacturerpartnumber">

        @Model.ManufacturerPartNumber
    </div>
}

I am using nopcommerce 2.2 & trying to use the partial view ProductVariant_SKU_Man_Stock in the _Productbox.cshtml but it comes up with the error:
The model item passed into the dictionary is of type

'System.Collections.Generic.List`1[Nop.Web.Models.Catalog.ProductModel+ProductVariantModel]',
but this dictionary requires a model item of type
'Nop.Web.Models.Catalog.ProductModel+ProductVariantModel'.

Below is the _Productbox.cshtml:

@model Nop.Web.Models.Catalog.ProductModel
<div class="product-item">
<h2 class="product-title">
@Model.Name
</h2>
<div class="description">
      @Html.Partial("_ProductVariant_SKU_Man_Stock",Model.ProductVariantModels)
    @Html.Raw(Model.FullDescription) <br />
    @Model.Size
</div>
<div class="add-info">
    <div class="prices">
        @Html.Partial("_ProductPrice", Model.ProductPrice)
    </div>
    <div class="buttons">
       @* <input type="button" value="@T("Products.Details")" class="productlistproductdetailbutton" onclick="setLocation('@Url.RouteUrl("Product", new { productId = Model.Id, SeName = Model.SeName })')" />
*@        @if (!Model.ProductPrice.DisableBuyButton)
        {
            <br />
            <input type="button" value="@T("ShoppingCart.AddToCart")" class="productlistaddtocartbutton" onclick="setLocation('@(@Url.RouteUrl("AddProductToCart", new { productId = Model.Id }))')" />
        }
     </div>
     </div>
     </div>

Is there any other way of doing it coz i need some fields within that partial view to display in this view.

productmodel.cs:

 public class ProductModel : BaseNopEntityModel
{
    public ProductModel()
    {
        ProductPrice = new ProductPriceModel();
       // ProductSku = new ProductSkuModel();
        DefaultPictureModel = new PictureModel();
        PictureModels = new List<PictureModel>();
        ProductVariantModels = new List<ProductVariantModel>();
        SpecificationAttributeModels = new List<ProductSpecificationModel>();
    }

    public string Name { get; set; }

    public string ShortDescription { get; set; }

    public string FullDescription { get; set; }

    public string ProductTemplateViewPath { get; set; }

    public string MetaKeywords { get; set; }

    public string MetaDescription { get; set; }

    public string MetaTitle { get; set; }

    public string SeName { get; set; }
    public string Size { get; set; }
    public ProductPriceModel ProductPrice { get; set; }
    public bool DefaultPictureZoomEnabled { get; set; }
    public PictureModel DefaultPictureModel { get; set; }
    public IList<PictureModel> PictureModels { get; set; }
    public IList<ProductVariantModel> ProductVariantModels { get; set; }
    public IList<ProductSpecificationModel> SpecificationAttributeModels { get; set; }
    public class ProductVariantModel : BaseNopEntityModel
    {
        public ProductVariantModel()
        {
            ProductSku = new ProductSkuModel();
            GiftCard = new GiftCardModel();
            ProductVariantPrice = new ProductVariantPriceModel();
            PictureModel = new PictureModel();
            AddToCart = new AddToCartModel();
            ProductVariantAttributes = new List<ProductVariantAttributeModel>();
        }

        public string Name { get; set; }

        public bool ShowSku { get; set; }

        public string Sku { get; set; }

        public string Description { get; set; }

        public bool ShowManufacturerPartNumber { get; set; }

        public string ManufacturerPartNumber { get; set; }

        public string DownloadSampleUrl { get; set; }

        public GiftCardModel GiftCard { get; set; }

        public string StockAvailablity { get; set; }

        public ProductVariantPriceModel ProductVariantPrice { get; set; }

        public AddToCartModel AddToCart { get; set; }

        public PictureModel PictureModel { get; set; }
        public ProductSkuModel ProductSku { get; set; }

        public IList<ProductVariantAttributeModel> ProductVariantAttributes { get; set; }
        public class ProductVariantPriceModel : BaseNopModel
        {
            public string OldPrice { get; set; }

            public string Price { get; set; }
            public string PriceWithDiscount { get; set; }

            public decimal PriceValue { get; set; }
            public decimal PriceWithDiscountValue { get; set; }

            public bool CustomerEntersPrice { get; set; }

            public bool CallForPrice { get; set; }

            public int ProductVariantId { get; set; }

            public bool HidePrices { get; set; }

            public bool DynamicPriceUpdate { get; set; }
        }
        public class ProductSkuModel : BaseNopModel
        {


            public string Sku { get; set; }

            public bool ShowSku { get; set; }
        }

Want to use this partial view :

@model Nop.Web.Models.Catalog.ProductModel.ProductVariantModel
@if (!String.IsNullOrWhiteSpace(Model.StockAvailablity))
{
    <div class="stock">

        @Model.StockAvailablity
    </div>
 }
<div class="clear">
</div>
@if (!String.IsNullOrWhiteSpace(Model.Sku) && Model.ShowSku)
{
    <div class="sku">

        @Model.Sku
    </div>
}
<div class="clear">
</div>
@if (!String.IsNullOrWhiteSpace(Model.ManufacturerPartNumber) && Model.ShowManufacturerPartNumber)
{
    <div class="manufacturerpartnumber">

        @Model.ManufacturerPartNumber
    </div>
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

简单 2024-12-23 16:08:33

还有一种替代方法,您可以将 ProductVariant_SKU_Man_Stock 中的库存数量等属性引入到 _Productbox.cshtml 中,而无需使用部分视图。

方式如下:

添加如下一组代码
Productmodel.cs中,有一个名为的嵌套类

public class ProductPriceModel : BaseNopModel
{
  //here declare stockquantity 
   public int stockqty { get; set; }
}

catalogcontroller.cs中,

有一个名为的函数

private ProductModel.ProductPriceModel PrepareProductPriceModel(Product product)
{ 
  //ADD THIS CODE BELOW
  model.stockqty=productvariants[0].StockQuantity;
}

_Productbox.cshtml中添加

@if (Model.ProductPrice.stockqty <= 0)
{
   <div class="innerrtcarts"> 
    insert out of stock message or image
    </div>
}

There is an alternative way in which you can bring the attributes like stock quantity in ProductVariant_SKU_Man_Stock into the _Productbox.cshtml,without using partial view.

The way is as follows:

Add the following set of code
In Productmodel.cs,there is a nested class called

public class ProductPriceModel : BaseNopModel
{
  //here declare stockquantity 
   public int stockqty { get; set; }
}

In catalogcontroller.cs,

there is a function called

private ProductModel.ProductPriceModel PrepareProductPriceModel(Product product)
{ 
  //ADD THIS CODE BELOW
  model.stockqty=productvariants[0].StockQuantity;
}

In _Productbox.cshtml add

@if (Model.ProductPrice.stockqty <= 0)
{
   <div class="innerrtcarts"> 
    insert out of stock message or image
    </div>
}
神回复 2024-12-23 16:08:33

_ProductVariant_SKU_Man_Stock 视图需要一个 ProductVariantModel,并且您要为其提供一个列表。如果您知道您只有一个变体,那么只需将该行更改为

@Html.Partial("_ProductVariant_SKU_Man_Stock",Model.ProductVariantModels.First())

The _ProductVariant_SKU_Man_Stock view is expected a single ProductVariantModel and you are giving it a list of them. If you know you will only have one Variant then just change that line to

@Html.Partial("_ProductVariant_SKU_Man_Stock",Model.ProductVariantModels.First())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文