【发布时间】:2017-11-05 09:56:30
【问题描述】:
我有一个 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 来检查当我按下Filter 按钮时我的Get 方法是否有效,我注意到它没有。有什么问题?
我从菜单中调用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 不会进行任何控制器调用,并且几乎不会使用您的模型渲染 html 视图。考虑改用 Html.RenderAction
标签: c# asp.net-mvc filtering partial-views