Adding dropdown and grid view functionality using asp.net core 2022
Loading
Adding dropdown and grid view functionality using asp.net core 2022
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jithu ThomasRank #437 · 5.7K reputationPosted Jan 28, 2024, 7:40 AM EST
Here is an example: -
// Item.cs public class Item { public int Id { get; set; } public string Name { get; set; } public string Category { get; set; } }// ItemsController.cs using Microsoft.AspNetCore.Mvc; using System.Collections.Generic; public class ItemsController : Controller { public IActionResult Index() { // Assume you have a list of items from a database or other source List items = GetItemsFromDatabase(); // For the dropdown, get distinct categories from the items List categories = items.Select(i => i.Category).Distinct().ToList(); ViewBag.Categories = categories; return View(items); } private List GetItemsFromDatabase() { // Logic to retrieve items from a database // For simplicity, creating a static list here return new List { new Item { Id = 1, Name = "Item 1", Category = "Category A" }, new Item { Id = 2, Name = "Item 2", Category = "Category B" }, new Item { Id = 3, Name = "Item 3", Category = "Category A" }, // Add more items as needed }; } }@model List Items Filter by Category: All @foreach (var category in ViewBag.Categories) { @category } ID Name Category @foreach (var item in Model) { @item.Id @item.Name @item.Category } function filterItems() { var category = document.getElementById('categoryDropdown').value; var rows = document.getElementById('itemsTable').getElementsByTagName('tbody')[0].getElementsByTagName('tr'); for (var i = 0; i