在剃刀内循环
我有分配给每个控件的字段名称“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;))
{
}
}
证明了这一点:
@foreach (var groups in Model.Groups)
{
foreach (var row in Model.Controls.ToList().Where(x => x.GroupTitle == groups.GroupTitle))
{
}
}
我想大概this answers也适用于你的情况。
我收到错误Lambda表达式作为动态调度操作的参数 – 2014-09-23 07:30:19
这可能是因为控件是IQueryable。我更新了将其转换为List的答案。 – DaniCE 2014-09-23 07:43:02
来测试你建议的链接我使用这个陈述@ { var layers =从Model.Controls中的p,其中p.GroupTitle =“GroupTitle”select p; } 但它给我错误查询表达式超过源类型'动态'或类型'动态'的连接序列是不允许的 – 2014-09-23 07:48:28
你能向我们解释一下你想达到的目标吗? – Marco 2014-09-23 07:20:44
请参阅我的修改后的帖子。 – 2014-09-23 07:27:06
我认为你的意思是'foreach(var在Model.Controls.Where(r => r.GroupTitle == groups.GroupTitle));' – 2014-09-23 07:28:13