09 - More MVC
09 - More MVC
Lecturer:
Ms Marinda Taljaard
Admin
▪Busy with marking – you will get feedback as soon as possible
• Remainder of prac 5
• Prac 6
• Prac 7 – marks already sent, updated marks will be sent with the next set of marks
• Semester test
1
2024/10/04
Practical 07 requirements
▪ Topic:
Your favourite Book Series / Streaming Service Series / Movie Series
▪ Update the Welcome and Privacy pages as follows:
• Welcome page: add some information about the topic you chose (e.g. info about Lord of the Rings
trilogy)
• Privacy page: Add your personal details (name, surname, student number and an image)
▪ Add a Model (instructions in tutorial)
▪ Add the Controller with the CRUD views – Create, Read, Update, Delete (instructions in
tutorial, make use of entity framework)
▪ Create the database table (instructions in tutorial) – table must have at least 4 fields
▪ Edit main navigation option text – add an option to the Index page of the new
controller
▪ Attempt to override some aspect of the css formatting – e.g. different background
colour
Prac 07 feedback
▪Marked / 20
• PDF file / 2
• Model & Database / 6
• Home Controller / 4 (Updating Index and Privacy Views)
• Movies Controller / 4
• Navigation / 2
• Formatting / 2
2
2024/10/04
Prac 07 feedback
▪Addingthe image to the privacy page resulted in some
pages with a broken image link
• Image cannot be found using the URL path provided
▪Preferred solution
• Addan images folder to the wwwroot folder, upload your
image(s) there, then use a URL path similar to:
•<img src=“~/images/picture.jpg” />
▪View
• Contains instructions on what the user should see
3
2024/10/04
4
2024/10/04
5
2024/10/04
▪Relationships
▪ https://www.entityframeworktutorial.net/code-first/configure-one-to-many-relationship-in-
code-first.aspx
6
2024/10/04
7
2024/10/04
8
2024/10/04
Model-View-Controller
▪ Each aspect from the domain needs its own database table
▪ Every database table will need its own controller – to provide the
functionality of the CRUD operations
▪ Every controller need a corresponding folder within the Views
folder where the specific files can be found, e.g.
• Model, Controller, Views to handle CRUD operations for movie
data
• Model, Controller, Views to handle CRUD operations for book
data
• Model, Controller, Views to handle CRUD operations for series
data
9
2024/10/04
HTMLHelper Class
▪ Renders html controls in the razor view, e.g.
• Binds the model object to HTML controls to display value of
model properties
• Assigns the value of the controls to the model properties
while submitting a web form
▪ @Html.DisplayNameFor(…) gets the
display name for the model
▪ @Html.DisplayFor(…) generates a html
string based on the value of the model property
10
2024/10/04
11
2024/10/04
12
2024/10/04
▪Post:
• Some error checking
• Save to table
• Return to Index view
▪HTTP POST
• When action method is called by something like button click event
• E.g. Save the updated information back to the database
• [HttpPost] required to specify that the second method can only be invoked for POST
requests
13
2024/10/04
14
2024/10/04
Validation attributes
▪ [Required]
▪ [RegularExpression]
▪ [Range]
▪ [Compare]
15
2024/10/04
Validation
▪ Look for corresponding code in
View
• E.g.
relevant when creating new
records
Consider DB options
▪With Web Forms you had an Access file that you could access from outside
the application
▪Using the EF functionality, the database is created within VS, and available
on your local machine
• This is a problem for handing in your project to be evaluated on another PC
• It is possible to specify initial “seed” data for database tables, which can then be run
from the Package Manager Console
16
2024/10/04
17
2024/10/04
Add migration
▪ Using Package Manager Console
18
2024/10/04
19
2024/10/04
Practical 08
▪ Start a new ASP.NET Core MVC project for Prac 8
• Include Individual user accounts
▪ Topic: Delivery Guy Web Application
• Tables for Area, Driver and Package (refer to the Access database on Moodle for guidance)
▪ Usingthe template application, you should have a working application from the start
▪ Keep the Home Controller and Index View
• Update the Index View
- Remove the current information, add some paragraphs about the Courier Guy Company,
add the image
• Remove the Privacy View
- Implying removing ALL traces of the privacy view
Practical 08
▪ Create an object class for each of the required database tables – you can use the
same fields as in the Access database
• This should result in a different table for each object class
▪ Create a controller with corresponding views for each of the model object classes
• All
CRUD (create, read, update, delete) functionality must be available for the Area, Driver and
Package tables
▪ Add at least 3 records to each of your database tables
• Using the method of providing the data in the ApplicationDbContext.cs file
20
2024/10/04
Practical 08
▪ Update the main navigation bar:
• Display a suitable application name on the left
• Ensure that your main navigation contains options for your user to access to each of
the tables in the database
▪ Add your details in the footer of the web application
▪ Save the word document as a pdf document in the root (top level
folder) of your web application
21
2024/10/04
Practical 08
▪When you are ready to submit:
• Click
on Build, Clean Solution
• Compress this version, and upload the compressed folder to Moodle
22