如何在单击搜索按钮后不重新设置控制值的情况下将过滤器应用于列表?

时间:2019-05-29 06:25:58

标签: c# asp.net-mvc asp.net-core razor-pages

我具有带有搜索按钮的表单中的“人员列表”和过滤控件。单击“搜索”按钮时,列表中将填充已过滤的数据,但搜索控件也会丢失其选择的值。我不确定这是否是最好的方法。

我尝试将视图分为部分视图和主视图,即“人员列表”位于部分视图中,而“过滤控件”位于主视图中。

应该过滤列表,而不丢失搜索控件的值。完成这项任务的最佳实践应该是什么?样本图片>> list with filtration controls

1 个答案:

答案 0 :(得分:0)

我建议先使用ViewBag将数据存储在控制器中,如下所示:

    public ActionResult YourSearchPage()
    {
        //Bind the data to a list here
        List<YourResultType> result = //Get from DB
        return View(result);
    }

    //When the user clicks the "Search" button
    [HttpPost]
    public ActionResult YourSearchPage(string keyword, int listingType...)
    {
        //Bind the data to a list here
        List<YourResultType> result = //Get from DB
        ViewBag.keyword = keyword;
        ViewBag.listingType = listingType;
        //Filter the list
        return View(result);
    }

然后在视图上,将值绑定到如下所示的控件:

<input type="text" id="keyword" class="keyword" value="@ViewBag.keyword" style="width: 80%;"/>