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

How to Apply filter to list without resetting control values after search button click?

我在带有搜索按钮的表单中有一个人员列表和过滤控件。单击搜索按钮时,列表中会填充过滤后的数据,但搜索控件也会丢失其选定的值。我不确定这是执行此操作的最佳方法。

我试过将视图拆分为部分视图和主视图,即人员列表在部分视图中,过滤控件在主视图中。

应在不丢失搜索控件值的情况下过滤列表。完成此任务的最佳做法应该是什么?示例图片 >> list with filtration controls

我建议先用ViewBag把数据存入controller,像这样:

    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%;"/>