2017-04-26 12 views
0

コントローラー内の情報の検索を変更したくないので、コントローラーではなくビュー内にwhere節を追加することは可能ですか?私は現在、レストランのリストを取得していますが、ログインしたユーザーの電子メールがレストランの電子メールと同じであるレストランのみを表示したいとします。ビュー内でリストを返すときにwhere節を追加する方法

<div class="col-xs-12"> 
    <table class="table table-condensed table-striped"> 
     <tr> 
      <th> 
       @Html.DisplayNameFor(model => model.Rating) 
      </th> 
      <th> 
       @Html.DisplayNameFor(model => model.RestaurantName) 
      </th>    
      <th> 
       @Html.DisplayNameFor(model => model.County) 
      </th>    
      <th> 
       @Html.DisplayNameFor(model => model.RestaurantType) 
      </th> 
      <th></th> 
     </tr> 

     @foreach (var item in Model) 
     { 
      <tr> 
       <td> 
        @Html.DisplayFor(modelItem => item.Rating) 
       </td> 
       <td> 
        @Html.DisplayFor(modelItem => item.RestaurantName) 
       </td> 
       <td> 
        @Html.DisplayFor(modelItem => item.County) 
       </td>      
       <td> 
        @Html.DisplayFor(modelItem => item.RestaurantType) 
       </td> 
       <td> 
        @Html.ActionLink("Edit", "Edit", new { id = item.RestaurantID }) | 
        @Html.ActionLink("Details", "Details", new { id = item.RestaurantID }) | 
        @Html.ActionLink("Delete", "Delete", new { id = item.RestaurantID }) 
       </td> 
      </tr> 
     } 

    </table> 
</div> 
+1

最高の選択肢はおそらくビューモデルを作成することでしょう –

+0

モデルのコードを共有できますか? – Usman

+0

コントローラでこれを行うための適切な場所。ビューは、渡されたデータを表示する役割を担います。したがって、1)コントローラーで必要のないデータを取得せず、2)表示したくないコントローラーにデータを渡さないでください。 – mason

答えて

3

あなたが本当にリストを取得するためのロジックを変更したくない場合は、ゲッターを使用して、指定されたリストを返す、Listプロパティとビューモデルを使用することができます。

public List<Restaurant> MyRestaurants 
{ 
    get { return Restaurants.Where(x => x.RestaurantEmail == UserEmail); } 
} 
関連する問題