Bonjour à tous,

Me formant à l'ASP.NET, je suis le tutoriel de réalisation d'une application MVC + BootStrap.

Je cherche à implémenter une liste des noms de bars dans ma vue pour créer une bière. (histoire d'associer des bières à des bars)

Cependant, je vous avoue que l'aspect de la structure de l'ASP.NET m'est un peu flou pour le moment. Donc comprendre la réalisation d'une telle fonctionnalité m'aiderait beaucoup.

Voici mon code :

BeerController :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using BeerChecking.Models;
 
namespace BeerChecking.Controllers
{
    public class BeersController : Controller
    {
        private readonly BeerCheckingContext _context;
 
        public BeersController(BeerCheckingContext context)
        {
            _context = context;
        }
 
        // GET: Beers
        public async Task<IActionResult> Index()
        {
            return View(await _context.Beer.ToListAsync());
        }
 
        // GET: Beers/Details/5
        public async Task<IActionResult> Details(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }
 
            var beer = await _context.Beer
                .FirstOrDefaultAsync(m => m.BeerId == id);
            if (beer == null)
            {
                return NotFound();
            }
            return View(beer);
        }
 
        // GET: Beers/Create
        public IActionResult Create()
        {
            return View();
        }
 
        // POST: Beers/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("BeerId,BeerName,BeerMark,BeerIsTasted,BeerComment,BeerLastTasted")] Beer beer)
        {
            if (ModelState.IsValid)
            {
                _context.Add(beer);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            return View(beer);
        }
 
        // GET: Beers/Edit/5
        public async Task<IActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }
 
            var beer = await _context.Beer.FindAsync(id);
            if (beer == null)
            {
                return NotFound();
            }
            return View(beer);
        }
 
        // POST: Beers/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(int id, [Bind("BeerId,BeerName,BeerMark,BeerIsTasted,BeerComment,BeerLastTasted")] Beer beer)
        {
            if (id != beer.BeerId)
            {
                return NotFound();
            }
 
            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(beer);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!BeerExists(beer.BeerId))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return RedirectToAction(nameof(Index));
            }
            return View(beer);
        }
 
        // GET: Beers/Delete/5
        public async Task<IActionResult> Delete(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }
 
            var beer = await _context.Beer
                .FirstOrDefaultAsync(m => m.BeerId == id);
            if (beer == null)
            {
                return NotFound();
            }
 
            return View(beer);
        }
 
        // POST: Beers/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteConfirmed(int id)
        {
            var beer = await _context.Beer.FindAsync(id);
            _context.Beer.Remove(beer);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }
 
        private bool BeerExists(int id)
        {
            return _context.Beer.Any(e => e.BeerId == id);
        }
    }
}
BeerModel:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
 
namespace BeerChecking.Models
{
    public class Beer
    {
        [Key]
        public int BeerId { get; set; }
        public string BeerName { get; set; }
        public int? BeerMark { get; set; }
        public bool? BeerIsTasted { get; set; }
        public string BeerComment { get; set; }
        [DataType(DataType.Date)]
        public DateTime? BeerLastTasted { get; set; }
    }
}
BarModel:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
 
namespace BeerChecking.Models
{
    public class Bar
    {
        [Key]
        public int      BarId       { get; set; }
        public string   BarName     { get; set; }
        public string   BarAddress  { get; set; }
        public int      BarMark     { get; set; }
        public string   BarComment  { get; set; }
    }
}
BeerCreateView:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
@model Beer
 
@{
    ViewData["Title"] = "Create";
}
 
<h2>Create</h2>
 
<h4>Beer</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Create">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="BeerName" class="control-label"></label>
                <input asp-for="BeerName" class="form-control" />
                <span asp-validation-for="BeerName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="BeerMark" class="control-label"></label>
                <input asp-for="BeerMark" class="form-control" />
                <span asp-validation-for="BeerMark" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="BeerIsTasted" class="control-label"></label>
                <input asp-for="BeerIsTasted" class="form-control" />
                <span asp-validation-for="BeerIsTasted" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="BeerComment" class="control-label"></label>
                <input asp-for="BeerComment" class="form-control" />
                <span asp-validation-for="BeerComment" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="BeerLastTasted" class="control-label"></label>
                <input asp-for="BeerLastTasted" class="form-control" />
                <span asp-validation-for="BeerLastTasted" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </form>
    </div>
</div>
 
<div>
    <a asp-action="Index">Back to List</a>
</div>
 
@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
Merci de votre lecture et de votre éventuelle aide.

PS : merci de m'épargner vos liens vers les tutoriels du site que j'ai déjà consulté et étudié, je le rappelle l'aspect me paraît un peu flou sur certains points.