2016-04-26 15 views
1

これは本当に基本的な質問ですが、私は答えを探しましたが、見つからないようです。これはVS2015を使ったScaffolded MVC5 edit.cshtmlを扱っています。エンティティフレームワークの足場と剃刀を使用したドロップダウンリストのMVCフィルタ

私は2つのテーブルの間に1:Mの関係を持ち、子テーブルは@ Html.DropDownListを親の外部キーとして持っています。その@ Html.DropDownListをフィルタリングする必要があります。これらは詳細です:

1つのAssociationListには多くのAgsweepParametersがあります。

AssociationListの主キーはAssociationListIdです したがって、AssociationListIdフィールドはAgsweepParameterテーブルの外部キーです。

EntityFrameworkとMVC5 Scaffoldingを使用して、Entity Frameworkをモデルとして使用してcrud views/controllersを作成しました。

そのドロップダウンのために生成された宣言は次のようになります。

  <div class="form-group"> 
       @Html.LabelFor(model => model.AssociationListId, "Association:", htmlAttributes: new { @class = "control-label col-md-2" }) 
       <div class="col-md-10"> 
        @Html.DropDownList("AssociationListId", null, htmlAttributes: new { @class = "form-control" }) 
        @Html.ValidationMessageFor(model => model.AssociationListId, "", new { @class = "text-danger" }) 
       </div> 
      </div> 

質問:どのように私はそのAssociationList.AssociationType ==「ACA」ようにドロップダウンリストをフィルタリングしますか?私はこのようになりますIndex.cshtml AssociationList上のテーブルの上に同様のフィルタを使用し

注:ここ

  @foreach (var item in Model.Where(row=>row.AssociationType=="ACA")) { 
       <tr> 
        <td> 
         @Html.DisplayFor(modelItem => item.Name) 
        </td> 
        <td> 
         @Html.ActionLink("Edit", "Edit", new { id=item.AssociationListId }) | 
         @Html.ActionLink("Delete", "Delete", new { id=item.AssociationListId }) 
        </td> 
       </tr> 
      } 

答えて

1

基本的なMVC。私は次のことをしなければならなかった:

コントローラーは、フィルターを定義し、ビューを定義するオブジェクトではないことを理解する。 は、したがって、私は公共のActionResult編集(int型?ID)用のコントローラに移動し、変更

  ViewBag.AssociationListId = new SelectList(filteredAssociations, "AssociationListId", "Name", agsweepParameter.AssociationListId); 

  IEnumerable<AssociationList> filteredAssociations = db.AssociationLists.Where(row => row.AssociationType == "ACA"); 
      ViewBag.AssociationListId = new SelectList(filteredAssociations, "AssociationListId", "Name", agsweepParameter.AssociationListId); 
関連する問題