【问题标题】:C# ASP.NET Cannot Sort the Search result from databaseC# ASP.NET 无法对数据库中的搜索结果进行排序
【发布时间】:2018-11-28 15:50:04
【问题描述】:

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

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

在这个控制器中,我展示了没有所有者的设备(UserID == 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 的搜索参数就会消失,它将对我的整个列表而不是搜索结果进行排序。

请帮我找出我的错。

【问题讨论】:

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


    【解决方案1】:

    你的问题很明显。当您点击排序链接时,您也必须发送过滤参数。

    所以按照以下方式操作: 将 search 参数保留在 ViewBag 中的 deviceList 操作中:

    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)
    

    【讨论】:

      【解决方案2】:

      只需将搜索更改为在此操作上搜索

      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());
                      }
      

      【讨论】:

        【解决方案3】:

        看起来当您单击这些链接以按名称/制造商/类型进行排序时,它没有提交表单,因此它只是将您重定向到该链接。当您单击搜索按钮时,它会将表单与搜索数据一起发送回您的控制器,但是 @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>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-09-19
          • 2021-08-19
          • 2011-10-16
          • 2018-11-04
          • 2021-04-06
          • 1970-01-01
          • 2021-01-23
          相关资源
          最近更新 更多