ASP.NET MVC3 Razor Querying inside a model (foreach within foreach)

后端 未结 1 1417
臣服心动
臣服心动 2021-01-25 13:26

I\'m running into an issue in the View with pulling data from different entities. Basically I have a bridging entity CategoryProduct that brings together Category and Product da

1条回答
  •  悲哀的现实
    2021-01-25 13:58

    Why can't you just change the viewmodel something like this

     public class MyProduct
        {   
            public int ProductId { get; set; }
            public string Title { get; set; }
            public virtual ICollection CategoryList { get; set; }
        }
    

    and view

    @model IEnumerable
    

    Index

    @Html.DisplayFor(modelItem => item.ProductId) @foreach (var item in Model.CategoryList) { }
    ProductCategories for Product
    @Html.DisplayFor(modelItem => item.Title) @Html.DisplayFor(modelItem => item.CategoryId)

    0 讨论(0)
提交回复
热议问题