如何在 asp.net 核心 3.1 mvc 中保留所有查询字符串值?

How do I persist all querystring values in asp.net core 3.1 mvc?

我有一个 asp.net 核心 3.1 mvc 项目,其中有一个搜索页面(视图)。我通过 Session 数据坚持搜索、过滤和排序标准值。这工作正常,并正确保存数据,但查询字符串不包含我需要的所有信息。

过滤器和搜索的值是通过表单和提交按钮选择的,但排序是通过单击搜索结果中的 header 列来选择的。例如:

   <form asp-controller="Companies" asp-action="Index" method="get">
        <p>
           <label>Company Name: </label>
           <select asp-for="CompanyName" asp-items="Model.CompanyName">
                  <option value="">All</option>
           </select> 
           <input type="submit" value="Find" />
        </p>
    </form>

<table class="table">
 <thead>
   <tr>
      <th>
         <a asp-action="Index" asp-route-sortOrder="CompanyName">@Html.DisplayNameFor(model => model.CompaniesListViewModelList[0].CompanyName)</a>
      </th>
   </tr>
 </thead>
 <tbody>
     @foreach (var item in Model.CompaniesListViewModelList)
     {
         <tr>
            <td>
                @Html.DisplayFor(modelItem => item.CompanyName)
            </td>
            <td>
                <a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
                <a asp-action="Details" asp-route-id="@item.Id">Details</a> |
                <a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
            </td>
         </tr>
     }
 </tbody>
</table>

如果在 header 中单击更改排序顺序,none 的过滤器等查询字符串值将出现在查询字符串中。

我需要这个的原因是 link 可以发送给其他人。

所以问题是,如何让所有使用过的查询字符串值出现在查询字符串中?

在Index action方法中,您可以使用ViewBag或ViewData来存储当前搜索值(CompanyName),然后将其传递到视图页面:

然后,对于排序链接,您可以使用asp-route-{value}将当前搜索值添加为路由参数,并通过 ViewData 或 ViewBag 绑定值。代码如下:

 <a asp-action="Index" asp-route-CompanyName="@ViewData["CurrentFilter"]" asp-route-sortOrder="@ViewData["CompanyNameSortParm"]">CompanyName</a>

示例代码如下:

型号:

public class Employee
{
    public int EmployeeId { get; set; }
    public string EmployeeName { get; set; } 
    public string CompanyName { get; set; }
     
}
public class Company
{
    public int CompanyId { get; set; }
    public string CompanyName { get; set; }
    public List<Employee> Employees { get; set; }
}
public class EmployeeVM
{
    public string CompanyName { get; set; }
    public List<Company> AllCompanies { get; set; }
    public List<Employee> Employees { get; set; }
}

控制器:

public class CompaniesController : Controller
{
    public IActionResult Index(string sortOrder, string CompanyName)
    { 
        ViewData["CompanyNameSortParm"] = String.IsNullOrEmpty(sortOrder) ? "companyname_desc" : "";
        ViewData["CurrentFilter"] = CompanyName;

        var employees = from s in repo.GetEmployees()
                       select s;
        if (!String.IsNullOrEmpty(CompanyName))
        {
            employees = employees.Where(s => s.CompanyName.Contains(CompanyName));
        }
        switch (sortOrder)
        {
            case "companyname_desc":
                employees = employees.OrderByDescending(s => s.CompanyName);
                break; 
            default:
                employees = employees.OrderBy(s => s.EmployeeId);
                break;
        }

        EmployeeVM vm = new EmployeeVM();
        vm.Employees = employees.ToList();
        vm.AllCompanies = repo.GetAllCompanies().ToList();

        return View(vm);
    }
}

索引浏览量:

@model netcore3.Data.EmployeeVM
 
<form asp-controller="Companies" asp-action="Index" method="get">
    <p>
        <label>Company Name: </label>
        <select asp-for="CompanyName" asp-items="@(new SelectList(Model.AllCompanies, "CompanyName","CompanyName"))">
            <option value="">All</option>
        </select>
        <input type="submit" value="Find" />
    </p>
</form>
<table class="table">
    <thead>
        <tr>
            <th>
                EmployeeName
            </th>
            <th>
                <a asp-action="Index" asp-route-CompanyName="@ViewData["CurrentFilter"]" asp-route-sortOrder="@ViewData["CompanyNameSortParm"]">CompanyName</a>
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model.Employees)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.EmployeeName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.CompanyName)
                </td>
                <td>
                    <a asp-action="Edit" asp-route-id="@item.EmployeeId">Edit</a> |
                    <a asp-action="Details" asp-route-id="@item.EmployeeId">Details</a> |
                    <a asp-action="Delete" asp-route-id="@item.EmployeeId">Delete</a>
                </td>
            </tr>
        }
    </tbody>
</table>

结果是这样的:

参考:

Tutorial: Add sorting, filtering, and paging - ASP.NET MVC with EF Core

Anchor Tag Helper in ASP.NET Core