使用多个搜索字符串 ASP.NET Core Visual Studio 2017 添加分页链接到视图 - 搜索字符串未被接管
Add paging links to View using multiple search strings ASP.NET Core Visual Studio 2017 - search string is not taken over
我正在开发一个参考示例的分页索引视图 Sorting, filtering, paging, and grouping - EF Core with ASP.NET Core MVC tutorial (3 of 10)
在我的索引页面上,我使用了 3 个搜索字符串。
排序效果很好,但没有搜索参数。
一旦我在输入搜索字符串后尝试排序,就没有任何效果。
应该接管搜索字符串。它没有发生。
插入搜索字符串后的分页也无法正常工作。名为 CurrentFilterxxx
的 ViewData 元素为视图提供了当前过滤器字符串。该值必须包含在分页链接中,以便在分页期间保持过滤器设置,并且必须在重新显示页面时将其恢复到文本框中。
就我而言,它不起作用。
这里是控制器的索引方法Class:
public async Task<IActionResult> Index(
string sortOrder,
string searchStringComune,
string searchStringProvincia,
string searchStringRegione,
string searchString,
string currentFilter,
int? page)
{
ViewData["CurrentSort"] = sortOrder;
ViewData["ComuneSortParm"] = String.IsNullOrEmpty(sortOrder) ? "comune" : "";
ViewData["ProvinciaSortParm"] = String.IsNullOrEmpty(sortOrder) ? "provincia" : "";
ViewData["RegioneSortParm"] = String.IsNullOrEmpty(sortOrder) ? "regione" : "";
ViewData["PostalCodeSortParm"] = String.IsNullOrEmpty(sortOrder) ? "CAP" : "";
ViewData["AbitantsSortParm"] = String.IsNullOrEmpty(sortOrder) ? "abitanti" : "";
if (searchStringComune != null ||
searchStringProvincia != null ||
searchStringRegione != null ||
searchString != null)
{
page = 1;
if (searchStringComune != null)
{
searchString = searchStringComune;
}
if (searchStringProvincia != null)
{
searchString = searchStringProvincia;
}
if (searchStringRegione != null)
{
searchString = searchStringRegione;
}
}
else
{
searchString = currentFilter;
}
ViewData["CurrentFilterComune"] = searchStringComune;
ViewData["CurrentFilterProvincia"] = searchStringProvincia;
ViewData["CurrentFilterRegione"] = searchStringRegione;
ViewData["CurrentFilter"] = searchString;
var comuni = from s in _context.Listacomuniitaliani
select s;
if (!String.IsNullOrEmpty(searchStringComune) ||
!String.IsNullOrEmpty(searchStringProvincia) ||
!String.IsNullOrEmpty(searchStringRegione))
{
comuni = comuni.Where(s => s.Comune.Equals(searchStringComune) ||
s.Provincia.Equals(searchStringProvincia) ||
s.Regione.Equals(searchStringRegione));
}
switch (sortOrder)
{
case "comune":
comuni = comuni.OrderByDescending(s => s.Comune);
break;
case "provincia":
comuni = comuni.OrderBy(s => s.Provincia);
break;
case "regione":
comuni = comuni.OrderBy(s => s.Regione);
break;
case "CAP":
comuni = comuni.OrderBy(s => s.Cap);
break;
case "abitanti":
comuni = comuni.OrderBy(s => s.Abitanti);
break;
default:
comuni = comuni.OrderBy(s => s.Comune);
break;
}
int pageSize = 7;
return View(await PaginatedList<Listacomuniitaliani>.CreateAsync(comuni.AsNoTracking(), page ?? 1, pageSize));
}
这是我的索引代码:
@model PaginatedList<MyProjectName.Models.Listacomuniitaliani>
@{ViewData["Title"] = "Index";}
<h2>Index</h2>
<p>
<a asp-action="Create">Create New</a>
</p>
<form asp-action="Index" method="get">
<div class="form-actions no-color">
<p>
Cerca un comune: <input type="text" name="SearchStringComune" value="@ViewData["currentFilterComune"]" />
<input type="submit" value="trova" class="btn btn-default" />
</p>
</div>
</form>
<form asp-action="Index" method="get">
<div class="form-actions no-color">
<p>
Seleziona una provincia: <input type="text" name="SearchStringProvincia" value="@ViewData["currentFilterProvincia"]" />
<input type="submit" value="trova" class="btn btn-default" />
</p>
</div>
<form asp-action="Index" method="get">
<div class="form-actions no-color">
<p>
Seleziona una regione: <input type="text" name="SearchStringRegione" value="@ViewData["currentFilterRegione"]" />
<input type="submit" value="trova" class="btn btn-default" />
</p>
</div>
<form asp-action="Index" method="get">
<div class="form-actions no-color">
<p>
<a asp-action="Index">Torna indietro alla lista</a>
</p>
</div>
<table class="table">
<thead>
<tr>
<th>
Istat
</th>
<th>
<a asp-action="Index" asp-route-sortOrder="@ViewData["ComuneSortParm"]"
asp-route-currentFilterComune="@ViewData["CurrentFilterComune"]">Comune</a>
</th>
<th>
<a asp-action="Index" asp-route-sortOrder="@ViewData["ProvinciaSortParm"]"
asp-route-currentFilterProvincia="@ViewData["CurrentFilterProvincia"]">Provincia</a>
</th>
<th>
<a asp-action="Index" asp-route-sortOrder="@ViewData["RegioneSortParm"]"
asp-route-currentFilterRegione="@ViewData["CurrentFilterRegione"]">Regione</a>
</th>
<th>
Prefisso
</th>
<th>
<a asp-action="Index" asp-route-sortOrder="@ViewData["PostalCodeSortParm"]">CAP</a>
</th>
<th>
CodFisco
</th>
<th>
<a asp-action="Index" asp-route-sortOrder="@ViewData["AbitantsSortParm"]">Abitanti</a>
</th>
<th>
Link
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Istat)
</td>
<td>
@Html.DisplayFor(modelItem => item.Comune)
</td>
<td>
@Html.DisplayFor(modelItem => item.Provincia)
</td>
<td>
@Html.DisplayFor(modelItem => item.Regione)
</td>
<td>
@Html.DisplayFor(modelItem => item.Prefisso)
</td>
<td>
@Html.DisplayFor(modelItem => item.Cap)
</td>
<td>
@Html.DisplayFor(modelItem => item.CodFisco)
</td>
<td>
@Html.DisplayFor(modelItem => item.Abitanti)
</td>
<td>
@Html.DisplayFor(modelItem => item.Link)
</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>
@{
var prevDisabled = !Model.HasPreviousPage ? "disabled" : "";
var nextDisabled = !Model.HasNextPage ? "disabled" : "";
}
<a asp-action="Index"
asp-route-sortOrder="@ViewData["CurrentSort"]"
asp-route-page="@(Model.PageIndex - 1)"
@*asp-route-currentFilter="@ViewData["CurrentFilter"]"*@
asp-route-currentFilterComune="@ViewData["CurrentFilterComune"]"
asp-route-currentFilterRegione="@ViewData["CurrentFilterRegione"]"
asp-route-currentFilterProvincia="@ViewData["CurrentFilterProvincia"]"
class="btn btn-default @prevDisabled">
Previous
</a>
<a asp-action="Index"
asp-route-sortOrder="@ViewData["CurrentSort"]"
asp-route-page="@(Model.PageIndex + 1)"
@*asp-route-currentFilter="@ViewData["CurrentFilter"]"*@
asp-route-currentFilterComune="@ViewData["CurrentFilterComune"]"
asp-route-currentFilterRegione="@ViewData["CurrentFilterRegione"]"
asp-route-currentFilterProvincia="@ViewData["CurrentFilterProvincia"]"
class="btn btn-default ifextDisabled">
Next
</a>
标签助手 asp-route-currentFilter
还没有 used.Instead,我使用了更具体的标签助手;例如 asp-route-currentFilterComune
.
感谢任何帮助。
我已经开始工作了。
为此,我必须重新设计控制器的一部分并对视图代码进行一些修改。
处理多个搜索字符串对我来说非常具有挑战性。
这是我的新控制器索引方法:
public async Task<IActionResult> Index(
string sortOrder,
string searchStringComune,
string searchStringProvincia,
string searchStringRegione,
string searchString,
string currentFilter,
int? page)
{
ViewData["CurrentSort"] = sortOrder;
ViewData["ComuneSortParm"] = String.IsNullOrEmpty(sortOrder) ? "comune" : "";
ViewData["ProvinciaSortParm"] = String.IsNullOrEmpty(sortOrder) ? "provincia" : "";
ViewData["RegioneSortParm"] = String.IsNullOrEmpty(sortOrder) ? "regione" : "";
ViewData["PostalCodeSortParm"] = String.IsNullOrEmpty(sortOrder) ? "CAP" : "";
ViewData["AbitantsSortParm"] = String.IsNullOrEmpty(sortOrder) ? "abitanti" : "";
if (searchStringComune != null ||
searchStringProvincia != null ||
searchStringRegione != null ||
searchString != null)
{
page = 1;
}
else
{
searchStringProvincia = currentFilter;
searchStringRegione = currentFilter;
foreach (ItalianRegions itemRegion in Enum.GetValues(typeof(ItalianRegions)))
{
if (searchStringRegione == itemRegion.ToString())
{
searchStringProvincia = null;
}
}
foreach (ItalianComunes itemComune in Enum.GetValues(typeof(ItalianComunes)))
{
if (searchStringProvincia == itemComune.ToString())
{
searchStringRegione = null;
}
}
}
ViewData["CurrentFilterComune"] = searchStringComune;
ViewData["CurrentFilterProvincia"] = searchStringProvincia;
ViewData["CurrentFilterRegione"] = searchStringRegione;
ViewData["CurrentFilter"] = searchString;
var comuni = from s in _context.Listacomuniitaliani
select s;
if (!String.IsNullOrEmpty(searchStringComune) ||
!String.IsNullOrEmpty(searchStringProvincia) ||
!String.IsNullOrEmpty(searchStringRegione))
{
comuni = comuni.Where(s => s.Comune.Equals(searchStringComune) ||
s.Provincia.Equals(searchStringProvincia) ||
s.Regione.Equals(searchStringRegione));
}
switch (sortOrder)
{
case "comune":
comuni = comuni.OrderByDescending(s => s.Comune);
break;
case "provincia":
comuni = comuni.OrderBy(s => s.Provincia);
break;
case "regione":
comuni = comuni.OrderBy(s => s.Regione);
break;
case "CAP":
comuni = comuni.OrderBy(s => s.Cap);
break;
case "abitanti":
comuni = comuni.OrderBy(s => s.Abitanti);
break;
default:
comuni = comuni.OrderBy(s => s.Comune);
break;
}
int pageSize = 7;
return View(await PaginatedList<Listacomuniitaliani>.CreateAsync(comuni.AsNoTracking(), page ?? 1, pageSize));
}
我已经使用 2 个不同的 enum
循环进入搜索字符串,以便能够在分页时正确设置搜索字段中的搜索字符串文本。
这里是 Index
代码:
@model PaginatedList<MyProject.Models.Listacomuniitaliani>
@{
ViewData["Title"] = "Index";
}
<h2>Index</h2>
<p>
<a asp-action="Create">Create New</a>
</p>
<form asp-action="Index" method="get">
<div class="form-actions no-color">
<p>
Cerca un comune: <input type="text" name="searchStringComune" value="@ViewData["CurrentFilterComune"]" />
<input type="submit" value="trova" class="btn btn-default" />
</p>
</div>
</form>
<form asp-action="Index" method="get">
<div class="form-actions no-color">
<p>
Seleziona una provincia: <input type="text" name="searchStringProvincia" value="@ViewData["CurrentFilterProvincia"]" />
<input type="submit" value="trova" class="btn btn-default" />
</p>
</div>
</form>
<form asp-action="Index" method="get">
<div class="form-actions no-color">
<p>
Seleziona una regione: <input type="text" name="searchStringRegione" value="@ViewData["CurrentFilterRegione"]" />
<input type="submit" value="trova" class="btn btn-default" />
</p>
</div>
</form>
<form asp-action="Index" method="get">
<div class="form-actions no-color">
<p>
<a asp-action="Index">Torna indietro alla lista</a>
</p>
</div>
</form>
<table class="table">
<thead>
<tr>
<th>
Istat
</th>
<th>
<a asp-action="Index" asp-route-sortOrder="@ViewData["ComuneSortParm"]">Comune</a>
</th>
<th>
<a asp-action="Index" asp-route-sortOrder="@ViewData["ProvinciaSortParm"]"
asp-route-currentFilter="@ViewData["CurrentFilterProvincia"]">Provincia</a>
</th>
<th>
<a asp-action="Index" asp-route-sortOrder="@ViewData["RegioneSortParm"]"
asp-route-currentFilter="@ViewData["CurrentFilterRegione"]">Regione</a>
</th>
<th>
Prefisso
</th>
<th>
<a asp-action="Index" asp-route-sortOrder="@ViewData["PostalCodeSortParm"]">CAP</a>
</th>
<th>
CodFisco
</th>
<th>
<a asp-action="Index" asp-route-sortOrder="@ViewData["AbitantsSortParm"]">Abitanti</a>
</th>
<th>
Link
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Istat)
</td>
<td>
@Html.DisplayFor(modelItem => item.Comune)
</td>
<td>
@Html.DisplayFor(modelItem => item.Provincia)
</td>
<td>
@Html.DisplayFor(modelItem => item.Regione)
</td>
<td>
@Html.DisplayFor(modelItem => item.Prefisso)
</td>
<td>
@Html.DisplayFor(modelItem => item.Cap)
</td>
<td>
@Html.DisplayFor(modelItem => item.CodFisco)
</td>
<td>
@Html.DisplayFor(modelItem => item.Abitanti)
</td>
<td>
@Html.DisplayFor(modelItem => item.Link)
</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>
@{
var prevDisabled = !Model.HasPreviousPage ? "disabled" : "";
var nextDisabled = !Model.HasNextPage ? "disabled" : "";
}
<a asp-action="Index"
asp-route-sortOrder="@ViewData["CurrentSort"]"
asp-route-page="@(Model.PageIndex - 1)"
asp-route-currentFilter=@if (@ViewData["CurrentFilterRegione"] != null)
{
@ViewData["CurrentFilterRegione"]
} else
{
@ViewData["CurrentFilterProvincia"]
}
class="btn btn-default @prevDisabled">
Previous
</a>
<a asp-action="Index"
asp-route-sortOrder="@ViewData["CurrentSort"]"
asp-route-page="@(Model.PageIndex + 1)"
asp-route-currentFilter=@if (@ViewData["CurrentFilterRegione"] != null)
{
@ViewData["CurrentFilterRegione"]
} else
{
@ViewData["CurrentFilterProvincia"]
}
class="btn btn-default @nextDisabled">
Next
</a>
现在可以完美运行了。排序工作。分页也有效,维护搜索字符串文本。
我正在开发一个参考示例的分页索引视图 Sorting, filtering, paging, and grouping - EF Core with ASP.NET Core MVC tutorial (3 of 10)
在我的索引页面上,我使用了 3 个搜索字符串。
排序效果很好,但没有搜索参数。 一旦我在输入搜索字符串后尝试排序,就没有任何效果。 应该接管搜索字符串。它没有发生。
插入搜索字符串后的分页也无法正常工作。名为 CurrentFilterxxx
的 ViewData 元素为视图提供了当前过滤器字符串。该值必须包含在分页链接中,以便在分页期间保持过滤器设置,并且必须在重新显示页面时将其恢复到文本框中。
就我而言,它不起作用。
这里是控制器的索引方法Class:
public async Task<IActionResult> Index(
string sortOrder,
string searchStringComune,
string searchStringProvincia,
string searchStringRegione,
string searchString,
string currentFilter,
int? page)
{
ViewData["CurrentSort"] = sortOrder;
ViewData["ComuneSortParm"] = String.IsNullOrEmpty(sortOrder) ? "comune" : "";
ViewData["ProvinciaSortParm"] = String.IsNullOrEmpty(sortOrder) ? "provincia" : "";
ViewData["RegioneSortParm"] = String.IsNullOrEmpty(sortOrder) ? "regione" : "";
ViewData["PostalCodeSortParm"] = String.IsNullOrEmpty(sortOrder) ? "CAP" : "";
ViewData["AbitantsSortParm"] = String.IsNullOrEmpty(sortOrder) ? "abitanti" : "";
if (searchStringComune != null ||
searchStringProvincia != null ||
searchStringRegione != null ||
searchString != null)
{
page = 1;
if (searchStringComune != null)
{
searchString = searchStringComune;
}
if (searchStringProvincia != null)
{
searchString = searchStringProvincia;
}
if (searchStringRegione != null)
{
searchString = searchStringRegione;
}
}
else
{
searchString = currentFilter;
}
ViewData["CurrentFilterComune"] = searchStringComune;
ViewData["CurrentFilterProvincia"] = searchStringProvincia;
ViewData["CurrentFilterRegione"] = searchStringRegione;
ViewData["CurrentFilter"] = searchString;
var comuni = from s in _context.Listacomuniitaliani
select s;
if (!String.IsNullOrEmpty(searchStringComune) ||
!String.IsNullOrEmpty(searchStringProvincia) ||
!String.IsNullOrEmpty(searchStringRegione))
{
comuni = comuni.Where(s => s.Comune.Equals(searchStringComune) ||
s.Provincia.Equals(searchStringProvincia) ||
s.Regione.Equals(searchStringRegione));
}
switch (sortOrder)
{
case "comune":
comuni = comuni.OrderByDescending(s => s.Comune);
break;
case "provincia":
comuni = comuni.OrderBy(s => s.Provincia);
break;
case "regione":
comuni = comuni.OrderBy(s => s.Regione);
break;
case "CAP":
comuni = comuni.OrderBy(s => s.Cap);
break;
case "abitanti":
comuni = comuni.OrderBy(s => s.Abitanti);
break;
default:
comuni = comuni.OrderBy(s => s.Comune);
break;
}
int pageSize = 7;
return View(await PaginatedList<Listacomuniitaliani>.CreateAsync(comuni.AsNoTracking(), page ?? 1, pageSize));
}
这是我的索引代码:
@model PaginatedList<MyProjectName.Models.Listacomuniitaliani>
@{ViewData["Title"] = "Index";}
<h2>Index</h2>
<p>
<a asp-action="Create">Create New</a>
</p>
<form asp-action="Index" method="get">
<div class="form-actions no-color">
<p>
Cerca un comune: <input type="text" name="SearchStringComune" value="@ViewData["currentFilterComune"]" />
<input type="submit" value="trova" class="btn btn-default" />
</p>
</div>
</form>
<form asp-action="Index" method="get">
<div class="form-actions no-color">
<p>
Seleziona una provincia: <input type="text" name="SearchStringProvincia" value="@ViewData["currentFilterProvincia"]" />
<input type="submit" value="trova" class="btn btn-default" />
</p>
</div>
<form asp-action="Index" method="get">
<div class="form-actions no-color">
<p>
Seleziona una regione: <input type="text" name="SearchStringRegione" value="@ViewData["currentFilterRegione"]" />
<input type="submit" value="trova" class="btn btn-default" />
</p>
</div>
<form asp-action="Index" method="get">
<div class="form-actions no-color">
<p>
<a asp-action="Index">Torna indietro alla lista</a>
</p>
</div>
<table class="table">
<thead>
<tr>
<th>
Istat
</th>
<th>
<a asp-action="Index" asp-route-sortOrder="@ViewData["ComuneSortParm"]"
asp-route-currentFilterComune="@ViewData["CurrentFilterComune"]">Comune</a>
</th>
<th>
<a asp-action="Index" asp-route-sortOrder="@ViewData["ProvinciaSortParm"]"
asp-route-currentFilterProvincia="@ViewData["CurrentFilterProvincia"]">Provincia</a>
</th>
<th>
<a asp-action="Index" asp-route-sortOrder="@ViewData["RegioneSortParm"]"
asp-route-currentFilterRegione="@ViewData["CurrentFilterRegione"]">Regione</a>
</th>
<th>
Prefisso
</th>
<th>
<a asp-action="Index" asp-route-sortOrder="@ViewData["PostalCodeSortParm"]">CAP</a>
</th>
<th>
CodFisco
</th>
<th>
<a asp-action="Index" asp-route-sortOrder="@ViewData["AbitantsSortParm"]">Abitanti</a>
</th>
<th>
Link
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Istat)
</td>
<td>
@Html.DisplayFor(modelItem => item.Comune)
</td>
<td>
@Html.DisplayFor(modelItem => item.Provincia)
</td>
<td>
@Html.DisplayFor(modelItem => item.Regione)
</td>
<td>
@Html.DisplayFor(modelItem => item.Prefisso)
</td>
<td>
@Html.DisplayFor(modelItem => item.Cap)
</td>
<td>
@Html.DisplayFor(modelItem => item.CodFisco)
</td>
<td>
@Html.DisplayFor(modelItem => item.Abitanti)
</td>
<td>
@Html.DisplayFor(modelItem => item.Link)
</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>
@{
var prevDisabled = !Model.HasPreviousPage ? "disabled" : "";
var nextDisabled = !Model.HasNextPage ? "disabled" : "";
}
<a asp-action="Index"
asp-route-sortOrder="@ViewData["CurrentSort"]"
asp-route-page="@(Model.PageIndex - 1)"
@*asp-route-currentFilter="@ViewData["CurrentFilter"]"*@
asp-route-currentFilterComune="@ViewData["CurrentFilterComune"]"
asp-route-currentFilterRegione="@ViewData["CurrentFilterRegione"]"
asp-route-currentFilterProvincia="@ViewData["CurrentFilterProvincia"]"
class="btn btn-default @prevDisabled">
Previous
</a>
<a asp-action="Index"
asp-route-sortOrder="@ViewData["CurrentSort"]"
asp-route-page="@(Model.PageIndex + 1)"
@*asp-route-currentFilter="@ViewData["CurrentFilter"]"*@
asp-route-currentFilterComune="@ViewData["CurrentFilterComune"]"
asp-route-currentFilterRegione="@ViewData["CurrentFilterRegione"]"
asp-route-currentFilterProvincia="@ViewData["CurrentFilterProvincia"]"
class="btn btn-default ifextDisabled">
Next
</a>
标签助手 asp-route-currentFilter
还没有 used.Instead,我使用了更具体的标签助手;例如 asp-route-currentFilterComune
.
感谢任何帮助。
我已经开始工作了。 为此,我必须重新设计控制器的一部分并对视图代码进行一些修改。 处理多个搜索字符串对我来说非常具有挑战性。
这是我的新控制器索引方法:
public async Task<IActionResult> Index(
string sortOrder,
string searchStringComune,
string searchStringProvincia,
string searchStringRegione,
string searchString,
string currentFilter,
int? page)
{
ViewData["CurrentSort"] = sortOrder;
ViewData["ComuneSortParm"] = String.IsNullOrEmpty(sortOrder) ? "comune" : "";
ViewData["ProvinciaSortParm"] = String.IsNullOrEmpty(sortOrder) ? "provincia" : "";
ViewData["RegioneSortParm"] = String.IsNullOrEmpty(sortOrder) ? "regione" : "";
ViewData["PostalCodeSortParm"] = String.IsNullOrEmpty(sortOrder) ? "CAP" : "";
ViewData["AbitantsSortParm"] = String.IsNullOrEmpty(sortOrder) ? "abitanti" : "";
if (searchStringComune != null ||
searchStringProvincia != null ||
searchStringRegione != null ||
searchString != null)
{
page = 1;
}
else
{
searchStringProvincia = currentFilter;
searchStringRegione = currentFilter;
foreach (ItalianRegions itemRegion in Enum.GetValues(typeof(ItalianRegions)))
{
if (searchStringRegione == itemRegion.ToString())
{
searchStringProvincia = null;
}
}
foreach (ItalianComunes itemComune in Enum.GetValues(typeof(ItalianComunes)))
{
if (searchStringProvincia == itemComune.ToString())
{
searchStringRegione = null;
}
}
}
ViewData["CurrentFilterComune"] = searchStringComune;
ViewData["CurrentFilterProvincia"] = searchStringProvincia;
ViewData["CurrentFilterRegione"] = searchStringRegione;
ViewData["CurrentFilter"] = searchString;
var comuni = from s in _context.Listacomuniitaliani
select s;
if (!String.IsNullOrEmpty(searchStringComune) ||
!String.IsNullOrEmpty(searchStringProvincia) ||
!String.IsNullOrEmpty(searchStringRegione))
{
comuni = comuni.Where(s => s.Comune.Equals(searchStringComune) ||
s.Provincia.Equals(searchStringProvincia) ||
s.Regione.Equals(searchStringRegione));
}
switch (sortOrder)
{
case "comune":
comuni = comuni.OrderByDescending(s => s.Comune);
break;
case "provincia":
comuni = comuni.OrderBy(s => s.Provincia);
break;
case "regione":
comuni = comuni.OrderBy(s => s.Regione);
break;
case "CAP":
comuni = comuni.OrderBy(s => s.Cap);
break;
case "abitanti":
comuni = comuni.OrderBy(s => s.Abitanti);
break;
default:
comuni = comuni.OrderBy(s => s.Comune);
break;
}
int pageSize = 7;
return View(await PaginatedList<Listacomuniitaliani>.CreateAsync(comuni.AsNoTracking(), page ?? 1, pageSize));
}
我已经使用 2 个不同的 enum
循环进入搜索字符串,以便能够在分页时正确设置搜索字段中的搜索字符串文本。
这里是 Index
代码:
@model PaginatedList<MyProject.Models.Listacomuniitaliani>
@{
ViewData["Title"] = "Index";
}
<h2>Index</h2>
<p>
<a asp-action="Create">Create New</a>
</p>
<form asp-action="Index" method="get">
<div class="form-actions no-color">
<p>
Cerca un comune: <input type="text" name="searchStringComune" value="@ViewData["CurrentFilterComune"]" />
<input type="submit" value="trova" class="btn btn-default" />
</p>
</div>
</form>
<form asp-action="Index" method="get">
<div class="form-actions no-color">
<p>
Seleziona una provincia: <input type="text" name="searchStringProvincia" value="@ViewData["CurrentFilterProvincia"]" />
<input type="submit" value="trova" class="btn btn-default" />
</p>
</div>
</form>
<form asp-action="Index" method="get">
<div class="form-actions no-color">
<p>
Seleziona una regione: <input type="text" name="searchStringRegione" value="@ViewData["CurrentFilterRegione"]" />
<input type="submit" value="trova" class="btn btn-default" />
</p>
</div>
</form>
<form asp-action="Index" method="get">
<div class="form-actions no-color">
<p>
<a asp-action="Index">Torna indietro alla lista</a>
</p>
</div>
</form>
<table class="table">
<thead>
<tr>
<th>
Istat
</th>
<th>
<a asp-action="Index" asp-route-sortOrder="@ViewData["ComuneSortParm"]">Comune</a>
</th>
<th>
<a asp-action="Index" asp-route-sortOrder="@ViewData["ProvinciaSortParm"]"
asp-route-currentFilter="@ViewData["CurrentFilterProvincia"]">Provincia</a>
</th>
<th>
<a asp-action="Index" asp-route-sortOrder="@ViewData["RegioneSortParm"]"
asp-route-currentFilter="@ViewData["CurrentFilterRegione"]">Regione</a>
</th>
<th>
Prefisso
</th>
<th>
<a asp-action="Index" asp-route-sortOrder="@ViewData["PostalCodeSortParm"]">CAP</a>
</th>
<th>
CodFisco
</th>
<th>
<a asp-action="Index" asp-route-sortOrder="@ViewData["AbitantsSortParm"]">Abitanti</a>
</th>
<th>
Link
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Istat)
</td>
<td>
@Html.DisplayFor(modelItem => item.Comune)
</td>
<td>
@Html.DisplayFor(modelItem => item.Provincia)
</td>
<td>
@Html.DisplayFor(modelItem => item.Regione)
</td>
<td>
@Html.DisplayFor(modelItem => item.Prefisso)
</td>
<td>
@Html.DisplayFor(modelItem => item.Cap)
</td>
<td>
@Html.DisplayFor(modelItem => item.CodFisco)
</td>
<td>
@Html.DisplayFor(modelItem => item.Abitanti)
</td>
<td>
@Html.DisplayFor(modelItem => item.Link)
</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>
@{
var prevDisabled = !Model.HasPreviousPage ? "disabled" : "";
var nextDisabled = !Model.HasNextPage ? "disabled" : "";
}
<a asp-action="Index"
asp-route-sortOrder="@ViewData["CurrentSort"]"
asp-route-page="@(Model.PageIndex - 1)"
asp-route-currentFilter=@if (@ViewData["CurrentFilterRegione"] != null)
{
@ViewData["CurrentFilterRegione"]
} else
{
@ViewData["CurrentFilterProvincia"]
}
class="btn btn-default @prevDisabled">
Previous
</a>
<a asp-action="Index"
asp-route-sortOrder="@ViewData["CurrentSort"]"
asp-route-page="@(Model.PageIndex + 1)"
asp-route-currentFilter=@if (@ViewData["CurrentFilterRegione"] != null)
{
@ViewData["CurrentFilterRegione"]
} else
{
@ViewData["CurrentFilterProvincia"]
}
class="btn btn-default @nextDisabled">
Next
</a>
现在可以完美运行了。排序工作。分页也有效,维护搜索字符串文本。