2017-06-05 65 views
0

我有一個Partial View,DropDownList。我有Main Viewmenu,我可以在這裏打電話給Partial View。我正在嘗試使用Partial View中的filtering以及來自它的參數。爲此,我需要將ddl的選定value@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

而是使用AjaxLink,使用簡單的錨網址爲一些數據屬性因爲您已經有代碼通過ajax顯式加載視圖! –

回答

0

需要輸入的類型更改爲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="button" id="add" name="add" value="Filter" /> 

    </div> 
</form> 

時,你的類型submit你的Ajax代碼將永遠不會被調用

+0

我試過了,但沒有發生任何事情。只需按下按鈕即可。在瀏覽器中檢查,但沒有任何反應。 –

+0

你需要添加一個ID到輸入:

+0

我已經添加了,也許別的東西? –