ASP.NET MVC How to automatize adding new categories and displaying filtered products

Viewed 25

I almost finished my Ecommerce shop project, almost because when I create new category, I would like it to appear in dropdown list in _layout section automatically.

I have done filtering in "normal user interface" controller, but it looks like huge redundancy, can I make one action instead of every time I add category, adding new action in controller and new a tag in layout? I tried also to somehow replace hard coded number with getting category object Id, but I can't figure out how to do that.

Here is filtered products by category code:

    public async Task<IActionResult> Keyboards()
    {
        var query = await _context.Products.Where(p => p.CategoryId == 1).ToListAsync();
        return View(query);
    }

    public async Task<IActionResult> Switches()
    {
        var query = await _context.Products.Where(p => p.CategoryId == 2).ToListAsync();
        return View(query);
    }

    public async Task<IActionResult> Cases()
    {
        var query = await _context.Products.Where(p => p.CategoryId == 3).ToListAsync();
        return View(query);
    }

    public async Task<IActionResult> Keycaps()
    {
        var query = await _context.Products.Where(p => p.CategoryId == 4).ToListAsync();
        return View(query);
    }

    public async Task<IActionResult> Plates()
    {
        var query = await _context.Products.Where(p => p.CategoryId == 5).ToListAsync();
        return View(query);
    }

    public async Task<IActionResult> Accesories()
    {
        var query = await _context.Products.Where(p => p.CategoryId == 6).ToListAsync();
        return View(query);
    }

And here is how I assign these actions in _layout:

<div class="dropdown-menu" aria-labelledby="navbarDropdown">
    <a class="dropdown-item text-dark" style="font-size: 1.1rem;" asp-area="" asp-controller="ProductsFilter" asp-action="Keyboards"><i class="bi bi-dot"></i>Klawiatury</a>
    <a class="dropdown-item text-dark" style="font-size: 1.1rem;" asp-area="" asp-controller="ProductsFilter" asp-action="Cases"><i class="bi bi-dot"></i>Obudowy</a>
    <a class="dropdown-item text-dark" style="font-size: 1.1rem;" asp-area="" asp-controller="ProductsFilter" asp-action="Switches"><i class="bi bi-dot"></i>Przełączniki</a>
    <a class="dropdown-item text-dark" style="font-size: 1.1rem;" asp-area="" asp-controller="ProductsFilter" asp-action="Keycaps"><i class="bi bi-dot"></i>Nasadki</a>
    <a class="dropdown-item text-dark" style="font-size: 1.1rem;" asp-area="" asp-controller="ProductsFilter" asp-action="Plates"><i class="bi bi-dot"></i>Płyty PCB</a>
    <a class="dropdown-item text-dark" style="font-size: 1.1rem;" asp-area="" asp-controller="ProductsFilter" asp-action="Accesories"><i class="bi bi-dot"></i>Akcesoria</a>
</div>

And models:

public class Category:IEntityBase
{
    public int Id { get; set; }
    [Display(Name = "Nazwa kategorii")]
    public string CategoryName { get; set; }

    // Relationships
    public List<Product> Products { get; set; }
}

public class Product:IEntityBase
{
    public int Id { get; set; }

    [Required]
    [Display(Name = "Nazwa")]
    [MaxLength(30, ErrorMessage = "Nazwa nie może posiadać więcej niż {1} znaków")]
    public string Name { get; set; }

    [Display(Name = "Opis")]
    [MinLength(20, ErrorMessage = "Opis nie może posiadać mniej niż {1} znaków")]
    public string Description { get; set; }

    [Display(Name = "Cena")]
    [DataType(DataType.Currency)]
    public double Price { get; set; }

    [Display(Name = "Ilość")]
    public int Quantity { get; set; }

    [Display(Name = "Adres URL zdjęcia")]
    public string ImageURL { get; set; }

    // Relationships
    [Required]
    [Display(Name = "Kategoria")]
    public int CategoryId { get; set; }
    [Display(Name = "Kategoria")]
    public virtual Category Category { get; set; }
}
0 Answers
Related