Index.cshtml.cs 1.17 KB
Newer Older
Andres Käver's avatar
exam  
Andres Käver committed
1
2
3
4
5
6
7
8
9
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using DAL.App.EF;
using Domain;
Andres Käver's avatar
Andres Käver committed
10
using Microsoft.AspNetCore.Mvc.Rendering;
Andres Käver's avatar
exam  
Andres Käver committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24

namespace WebApp.Pages.RecipeTags
{
    public class IndexModel : PageModel
    {
        private readonly DAL.App.EF.ApplicationDbContext _context;

        public IndexModel(DAL.App.EF.ApplicationDbContext context)
        {
            _context = context;
        }

        public IList<RecipeTag> RecipeTag { get;set; } = default!;

Andres Käver's avatar
Andres Käver committed
25
26
27
        [BindProperty(SupportsGet = true)]
        public int? RecipeId { get; set; }
        
Andres Käver's avatar
exam  
Andres Käver committed
28
29
        public async Task OnGetAsync()
        {
Andres Käver's avatar
Andres Käver committed
30
31
32
            ViewData["RecipeId"] = new SelectList(_context.Recipes, "Id", "Description", RecipeId);

            var query = _context.RecipeTags
Andres Käver's avatar
exam  
Andres Käver committed
33
                .Include(r => r.Recipe)
Andres Käver's avatar
Andres Käver committed
34
35
36
37
38
39
40
41
                .Include(r => r.Tag).AsQueryable();

            if (RecipeId != null)
            {
                query = query.Where(m => m.RecipeId == RecipeId);
            }

            RecipeTag = await query.ToListAsync();
Andres Käver's avatar
exam  
Andres Käver committed
42
43
44
        }
    }
}