У меня вопрос по поводу pagedlist.
У меня есть сетка данных, которые я могу отфильтровать, используя выпадающий список и флажок. Это работает хорошо. Я использую «PagedList», который позволяет отображать только 10 строк на страницу в моей сетке данных. Это хорошо работает, когда я не использую фильтр. Например, когда я выбираю элемент в раскрывающемся списке, фильтр хорошо работает на первой странице, но если я нажимаю на 2-й странице, фильтр больше не работает. Для фильтрации данных я использую методы Post и ViewModel. Что я могу сделать?
Спасибо!
Вот мой взгляд:
@using (Html.BeginForm("Index", "Missions", FormMethod.Post))
{
<section>
<h3>Localisation</h3>
@*
<div class="form-group">
@Html.LabelFor(model => model.DirGeoSelected, "Direction geo", htmlAttributes: new { @class = "control-label col-md-8" })
<div class="col-md-4">
@Html.DropDownListFor(model => model.DirGeoSelected, Model.DirGeoList, htmlAttributes: new { onchange = "form.submit();", @class = "form-control" })
@Html.ValidationMessageFor(model => model.DirGeoSelected, "", new { @class = "text-danger" })
</div>
</div>
*@
<div class="form-group">
@Html.LabelFor(model => model.ProgrammeIdSelected, "Programme", htmlAttributes: new { @class = "control-label col-md-5" })
<div class="col-md-7">
@Html.DropDownListFor(model => model.ProgrammeIdSelected, Model.Programme, "All", htmlAttributes: new { onchange = "form.submit();", @class = "form-control" })
@Html.ValidationMessageFor(model => model.ProgrammeIdSelected, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CountryIdSelected, "Pays", htmlAttributes: new { @class = "control-label col-md-5" })
<div class="col-md-7">
@Html.DropDownListFor(model => model.CountryIdSelected, Model.Country, "All", htmlAttributes: new { onchange = "form.submit();", @class = "form-control"/*, data_url = Url.Action("GetCountries")*/ })
@Html.ValidationMessageFor(model => model.CountryIdSelected, "", new { @class = "text-danger" })
</div>
</div>
</section>
<section>
<h3>Personne</h3>
</section>
<section>
@*
<h3>Date</h3>
<div class="form-group">
<div class="col-md-8">
@Html.TextBox("SelectedDateStart", new {@class = "datepicker"})
</div>
</div>
<div class="form-group">
<div class="col-md-8">
@Html.TextBox("SelectedDateEnd", new { @class = "datepicker"})
</div>
</div>
*@
</section>
<section>
<h3>Type</h3>
<div class="col-md-12 form-check">
@Html.CheckBox("Support", true, new { onchange = "form.submit();", @class = "form-check-input" })
@Html.Label("Mission Support", htmlAttributes: new { @class = "control-label" })
@Html.CheckBox("Autre", true, new { onchange = "form.submit();", @class = "form-check-input" })
@Html.Label("Autre", htmlAttributes: new { @class = "control-label" })
</div>
</section>
<section>
<h3>Priority</h3>
<div class="col-md-12 form-check">
@Html.CheckBox("Support", true, new { onchange = "form.submit();", @class = "form-check-input" })
@Html.Label("Mission Support", htmlAttributes: new { @class = "control-label" })
@Html.CheckBox("Autre", true, new { onchange = "form.submit();", @class = "form-check-input" })
@Html.Label("Autre", htmlAttributes: new { @class = "control-label" })
</div>
</section>
<section>
<h3>Décision</h3>
<div class="col-md-12">
@Html.EditorFor(x => x.Decison)
</div>
</section>
<section>
<h3>Equipe</h3>
@*
<div class="form-group">
@Html.LabelFor(model => model.DirectionIdSelected, "Direction", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-8">
@Html.DropDownListFor(model => model.DirectionIdSelected, Model.DirectionList, htmlAttributes: new { onchange = "form.submit();", @class = "form-control" })
@Html.ValidationMessageFor(model => model.DirectionIdSelected, "", new { @class = "text-danger" })
</div>
</div>
*@
</section>
}
</nav>
<div id="content" class="container">
<h2>Missions</h2>
@* @using (Html.BeginForm("Index", "Missions", FormMethod.Get))
{
@Html.TextBox("searching")
<input type="submit" value="submit" />
}
*@
<p class="newmission">
<input type="button" class="btn btn-info" value="Nouvelle Mission" onclick="location.href='@Url.Action("Create", "Missions")'" />
</p>
<table class="table table-bordered">
<tr>
<th class="col-md-2">
@Html.ActionLink("Pays", "Index",
new { SortOrder = Model.SortCountry})
</th>
<th class="col-md-1">
@Html.ActionLink("Date", "Index",
new { SortOrder = ViewBag.ListorderDate,
/*SelectedDesk = ViewBag.ListorderPays,
SelectedProgramme = ViewBag.ListorderDate,*/
})
</th>
<th class="col-md-2">
Personne
</th>
<th class="col-md-10">
Missions
</th>
<th class="col-md-10">
</th>
</tr>
@foreach (var item in Model.OnePageOfMissions)
{
<tr>
<td class="col-md-1">
@Html.DisplayFor(modelItem => item.decision)
</td>
<td class="col-md-2">
@Html.DisplayFor(modelItem => item.organization_hi_country.name_en)
</td>
<td class="col-md-1">
@Html.DisplayFor(modelItem => item.asked_date)
</td>
<td class="col-md-2"></td>
<td class="col-md-10">
<span class="typemission">Mission</span> @Html.DisplayFor(modelItem => item.list_type.name_en, new { @class = "typemission" }) : @Html.DisplayFor(modelItem => item.list_nature.name_en, new { @class = "typemission" })
<div>
<!-- les boutons d'actions -->
<a href="#@Html.DisplayFor(modelItem => item.id)" data-toggle="collapse" aria-expanded="false" aria-controls="@Html.DisplayFor(modelItem => item.id)">details</a>
</div>
<!-- le contenu masqué -->
<section id="@Html.DisplayFor(modelItem => item.id)" class="collapse">
<div class="well">
<p><em>Statut: </em>@Html.DisplayFor(modelItem => item.list_statut.name_en)</p>
<p><em>Durée: </em>@Html.DisplayFor(modelItem => item.duration) jours</p>
<p><em>Flexibilité: </em>@Html.DisplayFor(modelItem => item.list_flexibility.name_en) </p>
<p><em>Priorité: </em>@Html.DisplayFor(modelItem => item.list_priority.name_en) </p>
<p><em>Commentaires: </em>@Html.DisplayFor(modelItem => item.comments) </p>
</div>
</section>
</td>
<td class="col-md-10">
@Html.ActionLink("Edit", "Create", new { id = item.id })
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
@Html.PagedListPager((IPagedList)Model.OnePageOfMissions, page => Url.Action("Index", new { searchItem = ViewBag.searchItem, page }))
</table>
Вот мой контроллер:
namespace MissionsDF.Controllers
{
открытый класс MissionsController: контроллер
{
private Missions_devEntities db = new Missions_devEntities ();
// GET: /Missions/
public ActionResult Index(string SortOrder, int? page)
{
IndexViewModel model = new IndexViewModel();
//Affichage de la liste des Programmes
model.Programme = GetProgrammes();
//Tri des pays:
model.SortCountry = String.IsNullOrEmpty(SortOrder) ? "ListorderPays_desc" : "";
model.missionsList = db.missions_supportmission.ToList();
switch (SortOrder)
{
case "ListorderPays_desc":
model.missionsList = model.missionsList.OrderByDescending(s => s.organization_hi_country.name_en);
break;
default:
model.missionsList = model.missionsList.OrderBy(s => s.organization_hi_country.name_en);
break;
}
//Pagination
var pageNumber = page ?? 1; // if no page was specified in the querystring, default to the first page (1)
var onePageOfMissions = model.missionsList.ToPagedList(pageNumber, 10); // will only contain 10 products max because of the pageSize(equel to 10)
model.OnePageOfMissions = onePageOfMissions;
//Ischecked
var allDecisions = db.list_decision.ToList();//returns List<list_decision>
var checkBoxListItems = new List<CheckBoxListItem>(); //nouvelle instance de la classe checkboxlist
foreach (var decison in allDecisions)
{//On assigne les valeurs "id", "display" et "is checked" à la variable checkboxlistitem
checkBoxListItems.Add(new CheckBoxListItem()
{
ID = decison.decision_id,
Display = decison.name_en,
IsChecked = false //On the add view, no decision are selected by default
});
}
model.Decison = checkBoxListItems;
return View(model);
}
private List<SelectListItem> GetProgrammes()
{
return db.organization_programme
.Select(x => new SelectListItem
{
Value = x.prog_id,
Text = x.name_en
})
.ToList();
}
//POST: /Missions/Index
[HttpPost]
public ActionResult Index(IndexViewModel model, int? page)
{
//Filtres sur Programme et Country
if (ModelState.IsValid)
{
var pageNumber = page ?? 1;
var selecteddecision = model.Decison.Where(x => x.IsChecked).Select(x => x.ID);
//Si selecteddecision n'est pas null
//Si Programme n'est pas null et que country est null
if (!(selecteddecision.IsAny()))
{
if (!String.IsNullOrEmpty(model.ProgrammeIdSelected) && String.IsNullOrEmpty(model.CountryIdSelected))
{
var onePageOfMissions = db.missions_supportmission
.Where(a => a.programme_id == model.ProgrammeIdSelected)
.OrderBy(a => a.programme_id)
.Select(s => s).ToPagedList(pageNumber, 10);
model.OnePageOfMissions = onePageOfMissions;
}
//Si Country est null
else if (!String.IsNullOrEmpty(model.CountryIdSelected))
{
model.OnePageOfMissions = db.missions_supportmission
.Where(a => a.country_id == model.CountryIdSelected)
.OrderBy(a => a.country_id)
.Select(s => s).ToPagedList(pageNumber, 10);
if (model.missionsList != null)
{
var onePageOfMissions = model.missionsList.ToPagedList(pageNumber, 10);
model.OnePageOfMissions = onePageOfMissions;
}
}
//Sinon on affiche tout
else
{
model.OnePageOfMissions = db.missions_supportmission
.OrderBy(a => a.programme_id)
.Select(s => s).ToPagedList(pageNumber, 10);
}
}
else
{
foreach (var item in selecteddecision)
{
//Si Programme n'est pas null et que country est null
if (!String.IsNullOrEmpty(model.ProgrammeIdSelected) && String.IsNullOrEmpty(model.CountryIdSelected))
{
model.OnePageOfMissions = db.missions_supportmission
.Where(a => a.programme_id == model.ProgrammeIdSelected)
.Where(a => a.decision == item)
.OrderBy(a => a.programme_id)
.Select(s => s).ToPagedList(pageNumber, 10);
var onePageOfMissions = model.missionsList.ToPagedList(pageNumber, 10);
model.OnePageOfMissions = onePageOfMissions;
}
//Si Country est null
else if (!String.IsNullOrEmpty(model.CountryIdSelected))
{
model.OnePageOfMissions = db.missions_supportmission
.Where(a => a.country_id == model.CountryIdSelected)
.Where(a => a.decision == item)
.OrderBy(a => a.country_id)
.Select(s => s).ToPagedList(pageNumber, 10);
var onePageOfMissions = model.missionsList.ToPagedList(pageNumber, 10);
model.OnePageOfMissions = onePageOfMissions;
}
//Sinon on affiche tout
else
{
model.OnePageOfMissions = db.missions_supportmission
.Where(a => a.decision == item)
.OrderBy(a => a.programme_id)
.Select(s => s).ToPagedList(pageNumber, 10);
}
}
}
}
var allDecisions = db.list_decision.ToList();//returns List<list_decision>
var checkBoxListItems = new List<CheckBoxListItem>(); //nouvelle instance de la classe checkboxlist
foreach (var decison in allDecisions)
{//On assigne les valeurs "id", "display" et "is checked" à la variable checkboxlistitem
checkBoxListItems.Add(new CheckBoxListItem()
{
ID = decison.decision_id,
Display = decison.name_en,
});
}
model.Decison = checkBoxListItems;
//Affichage en cascade liste de programmes + liste pays
model.Programme = GetProgrammes();
model.Country = db.organization_hi_country
.Where(a => a.prog_id == model.ProgrammeIdSelected)
.Select(x => new SelectListItem
{
Value = x.country_id,
Text = x.name_en
}).ToList();
return View(model);
}