Mr A
Mr A

Reputation: 6778

using nested class within class doesnt works in mvc 3

Hi I have included nested class within a class to use it in a view but it doesnt show up the properties of that nested class below is the class, I want to use sku in the view : View:

@model Nop.Web.Models.Catalog.CategoryModel
<div class="product-item">
<h2 class="product-title">
@Model.Name
 </h2>

<div class="description">
      **@Model.ProductVariantModels.Select(x => x.Sku)//doesnt works** // partial class productvariant

</div>
<div class="add-info">
@Model.Name <br/> @Model.FullDescription //values from class CategoryModel 
  </div>
</div>

Model:

public class CategoryModel : BaseEntityModel
{
    public CategoryModel()
    {
        ProductVariantModels = new List<ProductVariantModel>();
    }
public string Name { get; set; }
public string FullDescription { get; set; }
public string MetaKeywords { get; set; }
public string MetaDescription { get; set; }
public string MetaTitle { get; set; }
public IList<ProductVariantModel> ProductVariantModels { get; set; }

public class ProductVariantModel : BaseNopEntityModel
    {
    public string Name { get; set; }
    public bool ShowSku { get; set; }
    public string Sku { get; set; }
    public string Description { get; set; }
    }

}

Upvotes: 0

Views: 1048

Answers (2)

drzaus
drzaus

Reputation: 25034

If you're using HtmlHelpers, you have to use a for loop with an index rather than a foreach loop.

As explained really well here, the automatic model binding expects field input names to have a 'dot' notation like Property.SubProperty.SubSub... to match the instance properties when assigning -- but if you render them in a foreach loop they won't have the full expression, and thus won't output the full 'dot' notation.

Also see MVC 4 binding nested list of lists result

Upvotes: 0

Daniel A. White
Daniel A. White

Reputation: 190996

ProductVariantModels is a List. You have to enumerate the List.

@foreach (var pvModel in Model.ProductVariantModels) {
   @pvModel.Sku
}

Upvotes: 4

Related Questions