I have a PartialView, that displays items in table. And I want to filter them with some criteria. My view:
@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>
My controller:
[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);
}
This way of filtering works fine with normal View
. But when I try to do the same with Partial View
it doesn't work, the page just reloads. I put break point
to check, if my Get method works when I press Filter
button, and I noticed that it doesn't. What is the problem?
I am calling Partial View
from menu with:
@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>