Я пытаюсь отфильтровать таблицу в представлении индекса по определенным значениям, для этого я использую метод индекса [HttpGet], который создает 2 списка выбора (по одному для каждого типа фильтра).Предполагается, что кнопка фильтра берет выбранное значение каждого списка и отправляет их в метод индекса [HttpPost], который фильтрует таблицу.
Проблема в том, что она не "сбрасывает" URL-адрес, когда яфильтруется, поэтому он добавляет к URL каждый раз, когда я меняю фильтр.
Индекс Получить (Этот работает нормально)
[HttpGet]
public IActionResult Index()
{
IEnumerable<Lesson> Lessons = _LessonRepository.GetAll();
ViewData["Types"] = GetTypesAsSelectList();
ViewData["Difficulties"] = GetDifficultiesAsSelectList();
return View(Lessons);
}
Индекс Пост (Этот постоянно добавляет / Урок / Индекс каждый раз, когда я нажимаю кнопку фильтра в моем представлении)
[HttpPost]
public IActionResult Index(string filter, string filter2)
{
IEnumerable<Les> Lessons = null;
if (filter == null && filter2 == null)
Lessons = _LessonRepository.GetAll();
else if (filter != null && filter2 == null)
{
Lessons = _LessonRepository.GetByDifficulty(filter);
}
else if (filter == null && filter2 != null)
{
Lessons = _LessonRepository.GetByType(filter2);
}
else if (filter != null && filter2 != null)
{
Lessons = _LessonRepository.GetByType(filter2).Where(l => l.Difficulty == filter);
}
ViewData["Types"] = GetTypesAsSelectList();
ViewData["Difficulties"] = GetDifficultiesAsSelectList();
return View(Lessons);
}
Просмотр
<form action="Lesson/Index/" method="post">
<div class="form-inline">
<select id="difficulties" name="filter" asp-items="@ViewData["Difficulties"] as List<SelectListItem>" class="form-control">
<option value="">-- Select difficulty --</option>
</select>
<select id="types" name="filter2" asp-items="@(ViewData["Types"] as List<SelectListItem>)" class="form-control">
<option value="">-- Select type --</option>
</select>
<button type="submit">Filter</button>
</div>
</form>