Фильтр был создан как метод действия контроллера, но я хочу сделать его глобальным.
Вот код фильтра — FilterController.cs:
Код: Выделить всё
public async Task Index(string postCategory)
{
ViewBag.PostCategory = postCategory;
var filteredPost = from p in _context.Posts select p;
if (postCategory == "All")
{
filteredPost = _context.Posts;
}
else if (!string.IsNullOrEmpty(postCategory))
{
filteredPost = filteredPost.Where(p => p.PostCategory == postCategory);
}
var filteredCategory = new CardsViewModel
{
PostCard = await filteredPost.ToListAsync()
};
_cache.Set("filteredCategory", filteredCategory);
string url = Request.Headers["Referer"].ToString();
return Redirect(url);
}
Пример контроллера, в котором я хочу использовать filteredPost вместо _context.Posts:
Код: Выделить всё
[HttpGet]
public async Task Index()
{
return View(await _context.Posts.ToListAsync());
}
Подробнее здесь: https://stackoverflow.com/questions/776 ... core-8-mvc
Мобильная версия