2014-09-23 73 views
0

我有分配給每個控件的字段名稱「GroupTitle」。我想遍歷每個分配給特定組的控制元素。在剃刀內循環

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   

} 

以下是我的代碼。我不知道如何訪問嵌套循環內的模型變量。這給了我錯誤。它也給我錯誤,Where子句不存在。

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

    } 
} 
+0

你能向我們解釋一下你想達到的目標嗎? – Marco 2014-09-23 07:20:44

+0

請參閱我的修改後的帖子。 – 2014-09-23 07:27:06

+1

我認爲你的意思是'foreach(var在Model.Controls.Where(r => r.GroupTitle == groups.GroupTitle));' – 2014-09-23 07:28:13

回答

1

證明了這一點:

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

    } 
} 

我想大概this answers也適用於你的情況。

+0

我收到錯誤Lambda表達式作爲動態調度操作的參數 – 2014-09-23 07:30:19

+0

這可能是因爲控件是IQueryable。我更新了將其轉換爲List的答案。 – DaniCE 2014-09-23 07:43:02

+0

來測試你建議的鏈接我使用這個陳述@ { var layers =從Model.Controls中的p,其中p.GroupTitle =「GroupTitle」select p; } 但它給我錯誤查詢表達式超過源類型'動態'或類型'動態'的連接序列是不允許的 – 2014-09-23 07:48:28