简体   繁体   English

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

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

I have an asp.net core 3.1 mvc project where I have a search page (view).我有一个 asp.net 核心 3.1 mvc 项目,其中有一个搜索页面(查看)。 I am persisting search, filter and sort criteria values via Session data.我通过 Session 数据持续搜索、过滤和排序标准值。 This works fine, and persists data correctly, but the querystring does not contain all the information I need it to.这可以正常工作,并且可以正确保存数据,但查询字符串不包含我需要的所有信息。

Values for the filters and search are selected via a form, and submit button, but sorting is selected by clicking the column headers in the search results.通过表单和提交按钮选择过滤器和搜索的值,但通过单击搜索结果中的列标题选择排序。 eg:例如:

   <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>

If the clicks in the header to change the sort order, none of the querystring values for the filters etc appear in the querystring.如果在 header 中单击以更改排序顺序,则过滤器等的查询字符串值都不会出现在查询字符串中。

The reason I need this is so that a link can be sent to someone else.我需要这个的原因是可以将链接发送给其他人。

So the question is, how do I get all the used querystring values to appear in the querystring?所以问题是,如何让所有使用的查询字符串值出现在查询字符串中?

In the Index action method, you could use ViewBag or ViewData to store the current search value (the CompanyName), then transfer it to the view page:在 Index action 方法中,您可以使用 ViewBag 或 ViewData 来存储当前搜索值(CompanyName),然后将其传输到视图页面:

Then, for the sort links, you could use asp-route-{value} to add the current search value as the route parameter, and bind values via the ViewData or ViewBag.然后,对于排序链接,您可以使用asp-route-{value}将当前搜索值添加为路由参数,并通过 ViewData 或 ViewBag 绑定值。 Code like this:像这样的代码:

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

The sample code as below:示例代码如下:

Models:楷模:

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

Controller: Controller:

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

Index Views:索引视图:

@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>

The result like this:结果是这样的:

在此处输入图像描述

Reference:参考:

Tutorial: Add sorting, filtering, and paging - ASP.NET MVC with EF Core 教程:添加排序、过滤和分页 - ASP.NET MVC with EF Core

Anchor Tag Helper in ASP.NET Core ASP.NET 内核中的锚标记助手

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 ASP.NET Core 3.1 MVC 中进行自定义路由 - How to do custom routing in ASP.NET Core 3.1 MVC 如何在 asp.net 核心 3.1 mvc 中使用带有子布局页面的 @section 脚本? - How do I use @section Scripts with child layout page in asp.net core 3.1 mvc? 如何在 asp.net 核心 3.1 mvc web 应用程序中将日志保存到数据库? - How do I save logs to database in asp.net core 3.1 mvc web application? 如何使用 ASP.NET Core 3.1 MVC 和 EF Core 对唯一字段使用数据验证注释? - How do use data validation annotation for unique fields with ASP.NET Core 3.1 MVC and EF Core? 如何修改 asp.net 核心中中间件的表单和查询字符串值? - How to modify form & querystring values at middleware in asp.net core? 如何使用 ASP.NET Core 从查询字符串中读取值? - How to read values from the querystring with ASP.NET Core? 我应该如何保护我的 Web 应用程序(ASP.Net Core 3.1 MVC)? - How should i secure my Web Application(ASP.Net Core 3.1 MVC)? 在 ASP.NET Core 3.1 MVC 中,如何加载 *.Views.dll - In ASP.NET Core 3.1 MVC, how can I load *.Views.dll 如何使用 ASP.NET Core 3.1 中的参数进行路由 - How to do routing with parameters in ASP.NET Core 3.1 ASP.NET Core 3.1 - 如何在经过身份验证后保留 JWT 令牌 - ASP.NET Core 3.1 - How to persist JWT Tokens once Authenticated
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM