2017-06-04 56 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'? –