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

后端 未结 1 1403
臣服心动
臣服心动 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<Category> CategoryList { get; set; }
        }
    

    and view

    @model IEnumerable<ProductsCode.Models.MyProduct>
    <h2>Index</h2>
    <table>
        <tr>
            <th>Product</th>
            @Html.DisplayFor(modelItem => item.ProductId) 
            <th>Categories for Product</th>
        </tr>
    
    @foreach (var item in Model.CategoryList) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Title)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CategoryId)
            </td>
        </tr>
    }
    </table>
    
    0 讨论(0)
提交回复
热议问题