Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"

What would my syntax look like ?

I hope that I am explaing this right.

Thanks

like image 730
G-Man Avatar asked Apr 27 '26 11:04

G-Man


1 Answers

 @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.

like image 176
Russ Cam Avatar answered Apr 29 '26 21:04

Russ Cam