2017-06-05 2 views
0

Partial ViewDropDownListがあります。そして私はMain Viewを持っています。menuで、私はこれをPartial Viewと呼ぶことができます。私はfilteringPartial Viewにして、パラメータを付けようとしています。これを行うには、(ddl)を@Ajax.ActionLinkにする必要があります。 jsで処理しようとしていますが、Partial Viewと呼ぶことなくページをリロードするだけです。メインビューからDropDownListの値を@Ajax.ActionLinkに設定します

ActionLinkの:

@Ajax.ActionLink(
"Parts", 
"PartsPartial", 
new 
{ 
    categorie = "add" 
}, 
new AjaxOptions 
{ 
    HttpMethod = "GET", 
    InsertionMode = InsertionMode.Replace, 
    UpdateTargetId = "content" 
}, new { @class = "button" } 
) 

フォームメソッドがフィルタリングを行うために部分図で取得:

<form method="get"> 
    <div> 
     <label>Category: </label> 
     @Html.DropDownList("categorie", Model.Categories as SelectList, 
     htmlAttributes: new { @class="form-control"}) 

     <label>Brand: </label> 
     @Html.DropDownList("brand", Model.Brands as SelectList, 
     htmlAttributes: new { @class="form-control" }) 
     <input type="submit" name="add" value="Filter" /> 

    </div> 
</form> 

そして、私のコントローラ:

public ActionResult PartsPartial(string categorie, int? brand) 
    { 
     string str = "add"; 
     List<bs_categories> categoriesList = _db.bs_categories.ToList(); 
     List<bs_brands> brandsList = _db.bs_brands.ToList(); 

     if (categorie == str) 
     { 
      IQueryable<bs_parts> prts = _db.bs_parts; 

      PartsViewModel pViewModel = new PartsViewModel 
      { 
       Parts = prts.ToList(), 
       Categories = new SelectList(categoriesList, "categories_id", "categories_name"), 
       Brands = new SelectList(brandsList, "brands_id", "brands_name") 
      }; 
      return PartialView(pViewModel); 
     } 

     decimal categoryId = Convert.ToDecimal(categorie); 

     var parts = _db.bs_parts.Where(x => x.parts_category_id == categoryId).OrderBy(x => x.parts_id); 

     PartsViewModel pvm = new PartsViewModel 
     { 
      Parts = parts.ToList(), 
      Categories = new SelectList(categoriesList, "categories_id", "categories_name"), 
      Brands = new SelectList(brandsList, "brands_id", "brands_name") 
     }; 
     return PartialView(pvm); 
    } 

そしてここでは、作成するJSです交換:

<script> 
    $(function() { 
     $('#add').click(function() { 
      var type = $('#categorie').val(); 

      $.ajax({ 
       url: this.href, 
       type: 'GET', 
       data: { type: type }, 
       cache: false, 
       success: function (result) { 
        $('#content').prepend(result); 
       } 
      }); 
      return false; 
     }); 
    }); 
</script> 

私は何かを逃したことがありますか?

+0

と呼ばれることはありませんを作る際に、いくつかのデータ属性としてURLを使用して簡単なアンカーを使用し、

<form method="get"> <div> <label>Category: </label> @Html.DropDownList("categorie", Model.Categories as SelectList, htmlAttributes: new { @class="form-control"}) <label>Brand: </label> @Html.DropDownList("brand", Model.Brands as SelectList, htmlAttributes: new { @class="form-control" }) <input type="button" id="add" name="add" value="Filter" /> </div> </form> 

をあなたは既にajaxを通して明示的にビューをロードするコードを持っているので! –

答えて

0

あなたはbuttonへの入力の種類を変更する必要があります:あなたはむしろAjaxLinkを使用してタイプsubmitあなたのAjaxコードが

+0

私は試しましたが、何も起こりませんでした。ちょうどボタンを押して、それがすべてです。ブラウザでは何も起こりませんが、検査します。 –

+0

あなたは入力にIDを追加する必要があります:

+0

私は既に何かを追加しましたか? –

関連する問題