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