2017-06-04 75 views
1

我有一個PartialView,它顯示錶中的項目。我想用一些標準來過濾它們。我的觀點:在部分視圖中過濾

@model Bike_Store.Models.PartsViewModel 

<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" value="Filter" />   
    </div> 
</form> 

<table>...</table> 

我的控制器:

[HttpGet] 
    public ActionResult PartsPartial(int? categorie, int? brand) 
    { 
     IQueryable<bs_parts> parts = _db.bs_parts.Include(p => p.bs_categories); 
     if (categorie != null && categorie != 0) 
     { 
      parts = parts.Where(p => p.parts_category_id == categorie); 
     } 
     if (brand != null && brand != 0) 
     { 
      parts = parts.Where(p => p.parts_brand_id == brand); 
     } 

     List<bs_categories> categoriesList = _db.bs_categories.ToList(); 
     List<bs_brands> brandsList = _db.bs_brands.ToList(); 

     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); 
    } 

過濾這種方式正常工作與正常View。但是,當我嘗試與Partial View一樣做時,它不起作用,頁面只是重新加載。我把break point檢查,如果我的Get方法工作,當我按Filter按鈕,我注意到它不。問題是什麼?

我打電話Partial View從菜單中有:

@Ajax.ActionLink(
"Parts", 
"PartsPartial", 
new 
{ 
    value1 = 1 
}, 
new AjaxOptions 
{ 
    HttpMethod = "GET", 
    InsertionMode = InsertionMode.Replace, 
    UpdateTargetId = "content" 
}, new { @class = "button" } 
) 
<div class="content" id="content"> 

</div> 
+0

@ Html.Partial()或@ Html.RenderPartial不做任何con troller調用,並且幾乎不能用你的模型呈現html視圖。 考慮使用Html.RenderAction代替 – Igor

回答

2

@ Html.Partial()或@ Html.RenderPartial不作任何控制器的呼叫,只是呈現HTML與你的模型。

考慮使用Html.RenderAction代替

UPDATE: 它看起來像有一個數字的答案類似的問題已經:

Html.Partial vs Html.RenderPartial & Html.Action vs Html.RenderAction

Html.Partial vs Html.RenderPartial & Html.Action vs Html.RenderAction. can any one please describe the difference

Controller for partial view in layout page MVC

+0

我有一個側面菜單,我用'@ Ajax.ActionLink'調用'部分視圖'。如何解決我的問題呢?你可以在我的文章中看到編輯 –

+1

你確定你的路由參數是value1 = 1而不是categorie = 1嗎? – Igor

+0

現在我明白了。有沒有辦法讓這個路由參數等於所選ddl項目的'id'?所以,當我選擇'categorie'並按下'Filter'按鈕時,它會顯示我已過濾的'Partial View'? –