在PartialView中过滤

时间:2017-06-04 14:29:04

标签: c# asp.net-mvc filtering partial-views

我有一个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>

1 个答案:

答案 0 :(得分:2)

@ Html.Partial()或@ Html.RenderPartial不进行任何控制器调用,只使用您的模型呈现html。

考虑使用Html.RenderAction

更新: 看起来已经有类似问题的答案有很多:

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