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
| @model ArticleModel
@{
ViewData["Title"] = "Edit";
}
<h1>Edition de l'article @Model.Nom</h1>
<div c&alss="card">
<div class="card-header">@Model.Nom</div>
<div class="card-body">
<p> Id: @Model.Id</p>
<p>Nom: @Model.Nom</p>
<p>Prix: @Model.Prix</p>
<p>Note: @Model.Note</p>
</div>
</div>
le fichier index.cshtml me donne cela:
@model List<ArticleModel>
@{
ViewData["Title"] = "Index";
}
<h1>Liste des articles</h1>
<div class="card">
<div class="card-header">Articles</div>
<div class="card-body">
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Nom</th>
<th>Prix</th>
<th>Note</th>
</tr>
</thead>
<tbody>
@foreach(var article in Model)
{
<tr>
<td>@article.Id</td>
<td><a asp-action="Edit" asp-route-id="@article.Id">@article.Nom</a></td>
<td>@article.Prix</td>
<td>@article.Note</td>
</tr>
}
</tbody>
</table>
</div>
</div>
</div>
Le fichier: ArticleController.cs donne ceci:
using Microsoft.AspNetCore.Mvc;
namespace WebA2.Controllers
{
public class ArticlesController : Controller
{
public List<ArticleModel>Articles { get; set; }
public ArticlesController()
{
Articles = new List<ArticleModel>
{
new ArticleModel
{
Id = 1,
Nom = "Télévision",
Prix = 499m,
Note = 4.75m
},
new ArticleModel
{
Id = 2,
Nom = "Tablette",
Prix = 299m,
Note = 3.98m
}
};
}
public IActionResult Index()
{
return View(Articles);
}
public IActionResult Edit(int id)
{
var article = Articles.Find(a => a.Id == id);
return View(article);
}
public IActionResult Delete(int id)
{
return View();
}
public IActionResult Create()
{
return View();
}
[HttpPost]
public IActionResult Create(ArticleModel article)
{
return RedirectToAction(nameof(Index));
}
public IActionResult Edit(ArticleModel article)
{
return RedirectToAction(nameof(Index));
}
[HttpPost]
public IActionResult Delete(ArticleModel article)
{
return RedirectToAction(nameof(Index));
}
}
} |
Partager