【问题标题】:Filtering in PartialView在 PartialView 中过滤
【发布时间】: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


【解决方案1】:

@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

【讨论】:

  • 我有一个侧边菜单,我用@Ajax.ActionLink 调用Partial View。如何解决我的问题?您可以在我的帖子中看到编辑
  • 您确定您的路由参数是 value1=1 而不是 categorie = 1?
  • 现在我看到了。有没有办法让这个 rout 参数等于所选 ddl 项目的id?所以,当我选择categorie 并按下Filter 按钮时,它会显示我已过滤Partial View?
  • 你的意思是像 { id: $('#categorie').val() } 吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多