Looping through models content in Razor

前端 未结 3 594
既然无缘
既然无缘 2021-02-05 16:39

I want to loop through each item in my model in my razor view but I want to group all items together. I then want to loop through each group. Imagine I have a table:

<         


        
3条回答
  •  别跟我提以往
    2021-02-05 17:13

    LINQ can help you do that

    @model IEnumerable
    
    
    @foreach (var item in Model.Select(i=>i.groupno).Distinct().ToList()) {
        
            
                
    Group No is @item
    @foreach (var grpName in Model.Where(i => i.groupno == item).ToList()) {

    GroupName: @grpName.groupName

    } }

提交回复
热议问题