在剃刀里面循环

时间:2014-09-23 07:11:53

标签: asp.net-mvc-4 razor

我有字段名称" GroupTitle"分配给每个控件。我想遍历分配给特定组的Control的每个元素。

public class Groups
{
    public virtual int Id { get; set; }
    public virtual string GroupTitle { get; set; }

}

public class Controls
{
    public int Id { get; set; } //Id
    public string Name { get; set; } //name of control/element
    public string ControlType { get; set; }  // checkbox, radio button, textbox, time, date
    public string Caption { get; set; } //caption/title/label        
    public string Content { get; set; } //in case of checkbox        
    public bool Mandatory { get; set; } //is mandatory to select or enter its value.                 
    public string GroupTitle { get; set; } // there will be title at the top of controls if grouped together                

    //public List<SelectListItem> SelectOptions { get; set; } //select/dropdown options e.g. Pakistan, Uk for country dropdown        

}

以下是我的代码。我不知道如何在嵌套循环中访问Model变量。这给了我错误。它也给我错误,Where子句不存在。

@foreach (var groups in Model.Groups)
{                                                    
    foreach (var row in Model.Controls.Where("GroupTitle ==",  @groups.GroupTitle;))
    {   

    }
}

1 个答案:

答案 0 :(得分:1)

证明这一点:

@foreach (var groups in Model.Groups)
{                                                    
    foreach (var row in Model.Controls.ToList().Where(x => x.GroupTitle == groups.GroupTitle))
    {   

    }
}

我认为this answers可能也适用于您的情况。