So far we've built mostly 'view-only' pages for our application. Now it's time to build some Create Read Update Delete (CRUD) pages.
Add a new class in the Controllers
folder called StoreManagerController
. Manually set it to inherit from Controller
.
Manually add these using statements to the top of the file.
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using Microsoft.VisualStudio.Web.CodeGenerators.Mvc.View;
using CopilotMvcMusicStore.Data;
using CopilotMvcMusicStore.Models;
In this new Controller use Copilot as follows:
-
Create an
Index
method that returns a View that contains allAlbum
entities, including Artist and Genre (you did this in an earlier exercise). -
Create a
Create
method that has no parameters and returns an empty View. The method should populate two ViewBag properties namedGenreId
andArtistId
which are assigned a newSelectList
populated from the appropriate database table.public ActionResult Create() { // add logic to create the album entity in the database ViewBag.GenreId = new SelectList(_context.Genres, "GenreId", "Name"); ViewBag.ArtistId = new SelectList(_context.Artists, "ArtistId", "Name"); return View(); }
-
Create a
Create
method that accepts an Album object via HTTP Post. If ModelState is valid, write the Album to the database and redirect to theIndex
view, otherwise popupulate your two ViewBag properties. This time make sure that theSelectList
selects the Genre and Artist based on theAlbum
argument passed to the method. Return the View and include theAlbum
. -
We need to create some views before we progress, so let's do that now.
-
Right-click in the
Index
method and create a new View namedIndex.cshtml
. Use the same approach as in Step 5.@model IEnumerable<CopilotMvcMusicStore.Models.Album> @{ ViewData["Title"] = "Index"; } <h2>Index</h2>
-
In the
Index.cshml
file use Copilot to add the following. If you tab and wait a while Copilot might do some magic...- A HTML paragraph containing an ActionLink that links to the
Create
method and has the title "Create New". - A HTML table with a header row containing the column titles "Title", "Genre", "Artist", "Price" and "Action".
- A foreach loop that iterates over the Model and renders a table row for each Album. The table row should contain the Album Title, Genre Name, Artist Name and Price with three Action Links - "Details", "Edit" and "Delete".
- A HTML paragraph containing an ActionLink that links to the
-
In your
StoreController
, right-click in theCreate
method and create a new View namedCreate.cshtml
. Select an empty view.@model CopilotMvcMusicStore.Models.Album @{ ViewData["Title"] = "Create"; } <h2>Create</h2> @using (Html.BeginForm()) { }
-
Use Copilot to add the following inside the
using
statement- A validation summary set to
true
- A HTML fiedset that provides an edit form for an
Album
entity, ensuring that the Genre and Artist are rendered as a drop-down lists.
- A validation summary set to
-
Add a DIV with an ActionLink taking back to the index.
Now run your solution and open the URL /StoreManager. You should be presented with a table of Albums. The Create New
link will take you to the page you just finished creating.
The challenge for you is to finish wiring up the Edit
, Details
and Delete
methods and views. You can use the Create
method as a template for the Edit
method.
Previous - Updating Browse and Details Views | Next - Wrapping things up