C#ASP.NET无法对数据库中的搜索结果进行排序

时间:2018-06-19 15:57:56

标签: c# asp.net asp.net-mvc sorting search

我有一个在C#ASP.NET中实现的设备管理应用程序,并且在我的应用程序中也实现了搜索和排序功能。

我的问题是我无法对搜索结果进行排序,而不是单击“名称”(按名称对结果进行排序)之后,它将对所有设备进行排序,而不仅仅是对搜索结果进行排序。

在此控制器中,我显示了没有所有者的设备(用户ID == null)

这是我的控制器:

public ActionResult deviceList(String sortBy, String search)
        {
            if (Session["userID"] != null)
            {
                ViewBag.SortNameParameter = String.IsNullOrEmpty(sortBy) ? "Name desc" : "";
                ViewBag.SortTypeParameter = sortBy == "Type" ? "Type desc" : "Type";
                ViewBag.SortManufacturerParameter = sortBy == "Manufacturer" ? "Manufacturer desc" : "Manufacturer";
                var devices = db.devices.AsQueryable();

                if (devices.Any(x => x.UserID == null))
                {
                    devices = db.devices.Where(x => x.UserID == null);

                    switch (sortBy)
                    {
                        case "Name desc":
                            devices = devices.Where(x => x.DName.Contains(search)).OrderByDescending(x => x.DName);
                            break;
                        ...
                        default:
                            devices = devices.Where(x => x.DName.Contains(search)).OrderBy(x => x.DName);
                            break;
                    }
                    return View(devices.ToList());
                }

缺少的零件按类型,制造商等进行相同的排序。

这是我的观点:

@using (Html.BeginForm("deviceList", "devices", FormMethod.Get))
                {
                    <label>
                        <b>Search:</b>
                        @Html.TextBox("Search")
                        <input type="submit" value="Search" />
                    </label>
                }
            </div>

            <table class="table table-striped custab">
                <thead>
                    <tr>
                        <th align="center">
                            @Html.ActionLink("Name", "deviceList", new { sortBy = ViewBag.SortNameParameter })
                        </th>
                        <th>
                            @Html.ActionLink("Manufacturer", "deviceList", new { sortBy = ViewBag.SortManufacturerParameter })
                        </th>
                        <th>
                            @Html.ActionLink("Type", "deviceList", new { sortBy = ViewBag.SortTypeParameter })
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model.DOS)
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model.DOSVersion)
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model.DProcessor)
                        </th>
                        <th align="center">
                            @Html.DisplayNameFor(model => model.DRAM)
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model.user.userName)
                        </th>
                        <th>Action</th>
                    </tr>
                    @foreach (var item in Model)
                    {
                        <tr>
                            <td>
                                @Html.DisplayFor(modelItem => item.DName)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.DManufacturer)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.DType)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.DOS)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.DOSVersion)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.DProcessor)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.DRAM)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.user.userName)
                            </td>

搜索功能很好用,但是在搜索结果之后,如果单击“名称”(进行排序),URL中的搜索参数就会消失,它将对整个列表而不是搜索结果进行排序。

请帮助我找出我的错。

3 个答案:

答案 0 :(得分:2)

您的问题很明显。单击排序链接时,还必须发送过滤器参数。

因此,请按照以下步骤操作: 还要在ViewBag操作的deviceList中保持 search 参数:

ViewBag.SearchParameter = search;
ViewBag.SortNameParameter = String.IsNullOrEmpty(sortBy) ? "Name desc" : "";
ViewBag.SortTypeParameter = sortBy == "Type" ? "Type desc" : "Type";
ViewBag.SortManufacturerParameter = sortBy == "Manufacturer" ? "Manufacturer desc" : "Manufacturer";

然后在链接中将其与sortBy相同地传递:

@Html.ActionLink("Name", "deviceList", new { search = ViewBag.SearchParameter, sortBy = ViewBag.SortNameParameter  })
@Html.ActionLink("Manufacturer", "deviceList", new { search = ViewBag.SearchParameter, sortBy = ViewBag.SortManufacturerParameter })
@Html.ActionLink("Type", "deviceList", new { search = ViewBag.SearchParameter, sortBy = ViewBag.SortTypeParameter })

并管理文本框的搜索参数:

@Html.TextBox("search", ViewBag.SearchParameter)

答案 1 :(得分:0)

只需将搜索更改为“搜索此操作”

public ActionResult deviceList(String sortBy, String Search)
        {
            if (Session["userID"] != null)
            {
                ViewBag.SortNameParameter = String.IsNullOrEmpty(sortBy) ? "Name desc" : "";
                ViewBag.SortTypeParameter = sortBy == "Type" ? "Type desc" : "Type";
                ViewBag.SortManufacturerParameter = sortBy == "Manufacturer" ? "Manufacturer desc" : "Manufacturer";
                var devices = db.devices.AsQueryable();

                if (devices.Any(x => x.UserID == null))
                {
                    devices = db.devices.Where(x => x.UserID == null);

                    switch (sortBy)
                    {
                        case "Name desc":
                            devices = devices.Where(x => x.DName.Contains(Search)).OrderByDescending(x => x.DName);
                            break;
                        ...
                        default:
                            devices = devices.Where(x => x.DName.Contains(Search)).OrderBy(x => x.DName);
                            break;
                    }
                    return View(devices.ToList());
                }

答案 2 :(得分:0)

当您单击这些链接以按名称/制造商/类型排序时,它似乎没有提交表单,因此仅将您重定向到该链接。当您单击搜索按钮时,它会将表单连同搜索数据一起发送回您的控制器,但是@Html.ActionLink(...)不会提交表单。

您可能需要将搜索文本与该链接一起发送,可能是通过将搜索文本绑定到模型并更新链接以包括该链接。请尝试以下操作:

@Html.ActionLink("Name", "deviceList", new { sortBy = ViewBag.SortNameParameter, ViewBag.SearchString })

在您的表单中,您需要对其进行更新以发送搜索字符串:

<label>
  <b>Search:</b>
  @Html.TextBox("Search", @ViewBag.SearchString)
  <input type="submit" value="Search" />
</label>