如何修改此搜索功能,以便我接受多个搜索字符串

时间:2018-03-05 10:10:18

标签: c# search asp.net-core pagination asp.net-core-mvc

我需要有关此代码的帮助

型号:

public class SearchList
{
    public string Name { get; set; }
    public string className { get; set; }
}

控制器:

   [Authorize]
    public IActionResult Search(string sortOrder, string currentFilter, string name, int? page, string className)
    {
        ViewData["CurrentSort"] = sortOrder;
        ViewData["NameSortParm"] = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
        ViewData["DateSortParm"] = sortOrder == "Date" ? "date_desc" : "Date";

        var searchList = FetchList()

        if (name != null)
        {
            page = 1;
        }
        else
        {
            name = currentFilter;
        }
        ViewData["CurrentFilter"] = name;

        if (!String.IsNullOrEmpty(name))
        {
            searchList = searchList.Where(m => m.Name.Contains(name)).ToList();

        }

        switch (sortOrder)
        {
            case "name_desc":
                searchList = searchList.OrderByDescending(s => s.Name).ToList();
                break;
            default:
                break;
        }

        int pageSize = 3;
        return View(PaginatedList<SearchList>.Create(searchList, page ?? 1, pageSize));

    }

    public IList<SearchList> FetchList()
    {
        var searchList = new List<SearchList>();

            searchList.Add(new SearchList
            {             
                Name = "Andy",
                className = "Class Alpha",

            });

            searchList.Add(new SearchList
            {             
                Name = "Nando",
                className = "Class Beta",

            });

            searchList.Add(new SearchList
            {             
                Name = "Anrya",
                className = "Class Alpha",

            });

        return searchList;
    }

我的观点

 <form asp-action="Search">
                <div>
                    <table id="t1" class="table table-bordered table-condensed">                            <tr>
                            <th>
                                <b>Details</b>
                            </th>
                        </tr>
                        <tr>
                            <td >
                                Name
                            </td>
                            <td>
           <input class="form-control" type="text" name="name" value="@ViewData["currentFilter"]" />

                   </td>
                        </tr>
                        <tr>
                            <td>
                                Class Name
                            </td>
                            <td>
                                <input class="form-control" style="width: 200px" type="text" name="classname">
                            </td>
                        </tr>
                    </table>
                    <div class="form-group">
                                <input type="submit" value="Search" class="btn btn-default"/>
                    </div>
                </div>
            </form>
        }
    </div>
</div>


         <form>
            <div>
                <table>
                    <tr>
                        <td colspan="6" class="tableheader">
                            Listing</b>
                        </td>
                    </tr>
                    <tr style="font-weight: bold; background:#efefef">
                        <td>
                            @Html.ActionLink("Name", "Search", new { sortOrder = ViewBag.NameSortParm })
                        </td>
                        <td>
                            Class Name
                        </td>
                    </tr>
                    @{
                        foreach (var m in Model.AsEnumerable())
                        {
                            <tr class="oddeventable">
                                <td>
                                    @Html.ActionLink(@m.Name, "TestStudent", "Student", new { Name = m.Name })
                                </td>
                                <td>
                                    @m.ClassName
                                </td>
                            </tr>
                        }
                    }
                </table>
            </div>
        </form>

@{
    var prevDisabled = !Model.HasPreviousPage ? "disabled" : "";
    var nextDisabled = !Model.HasNextPage ? "disabled" : "";
}

    <a asp-action="Search"
       asp-route-sortOrder="@ViewData["CurrentSort"]"
       asp-route-page="@(Model.PageIndex - 1)"
       asp-route-currentFilter="@ViewData["CurrentFilter"]"
       class="btn btn-default @prevDisabled">
        Previous
    </a>
    <a asp-action="Search"
       asp-route-sortOrder="@ViewData["CurrentSort"]"
       asp-route-page="@(Model.PageIndex + 1)"
       asp-route-currentFilter="@ViewData["CurrentFilter"]"
       class="btn btn-default @nextDisabled">
        Next
    </a>

当前视图和控制器只能接收一个搜索字符串“Name”,我想要做的是它也可以搜索className,所以基本上控制器将检索名称和className并过滤它并创建新的searchList并回发到View。我实际上很丢失,因为'currentFilter'本身就是'name'所以我不知道如何调整过滤器可以接受两个搜索字符串,所以稍后在View中我可以点击“Next”页面或者排序currentFilter。

1 个答案:

答案 0 :(得分:1)

看起来您已将className作为参数接受。我会删除currentFilter参数并添加/更改您的过滤:

MediaRecorder.AudioSource.VOICE_CALL

同时更改输入,以便通过正确的ViewData项默认值。请注意,您当前没有在className上设置值。

从你的下一个/上一个删除:

MediaRecorder.AudioSource.MIC

但是,你可以改为你的方法接受currentFilterName和currentFilterClassName,但是当你正在访问ViewData时,似乎没必要。