我有一個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>
@ Html.Partial()或@ Html.RenderPartial不做任何con troller調用,並且幾乎不能用你的模型呈現html視圖。 考慮使用Html.RenderAction代替 – Igor