[英]MVC 3 model foreach filter
I have the following razor syntax 我有以下剃刀语法
@{
foreach (var p in Model)
{
<b>@p.Age</b>
}
}
I would like to filter the foreach loop to only look at the Model records where p.City = "New York" 我想过滤foreach循环只看模型记录p.City =“New York”
What would my syntax look like ? 我的语法是什么样的?
I hope that I am explaing this right. 我希望我正在解释这一点。
Thanks 谢谢
@foreach (var p in Model.Where(i => i.City == "New York")) {
<b>@p.Age</b>
}
You might decide to do this filtering in the controller action, depending on whether you need other model records that don't have a city of "New York" in your view or not. 您可能决定在控制器操作中执行此过滤,具体取决于您是否需要在视图中没有“纽约”城市的其他模型记录。
You can use LINQ ( http://msdn.microsoft.com/en-us/library/bb397896.aspx ) extension methods like "Where" to apply the filter. 您可以使用LINQ( http://msdn.microsoft.com/en-us/library/bb397896.aspx )扩展方法,如“Where”来应用过滤器。 You also don't need the outer "@{}", you can just put an "@" in front of the foreach and Razor will figure out what you mean.
你也不需要外面的“@ {}”,你可以在foreach前放一个“@”,Razor会弄明白你的意思。 Something like:
就像是:
@foreach (var p in Model.Where(item => item.City == "New York")) {
<b>@p.Age</b>
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.