08 - Introduction to MVC (3)
08 - Introduction to MVC (3)
Getting started
▪Copy the MVCMovies folder to local storage (from Courses folder)
• \\cs2-new.mandela.ac.za\Courses\WRWV202
• Basic Movie Application with database
▪Open the Project MVCMovies in Visual studio (there is a sln file; File -
Open - Project)
▪Select to run the application
Getting started
▪ If
you see an error message about not being
able to show the application, try the
following:
▪ Select Tools, NuGet Package Manager,
Package Manager Console
▪ Type Update-Database at the command
prompt, press enter
▪ When the command has executed, run the
project again
1
2024/09/27
Getting started
▪Use the available links, see what the
current project offers
▪Close the run, return to the project in VS
▪Inspect the project – look at all the files
created, to determine things like:
• What types of files?
• What goes into each type of file?
• How is the syntax used different?
• Where does the formatting instructions come
from?
Lecturers:
Ms Marinda Taljaard & Ms Isabelle Taljaard
2
2024/09/27
3
2024/09/27
Model-View-Controller (MVC)
Model-View-Controller (MVC)
4
2024/09/27
Model-View-Controller (MVC)
▪Model
• Collection of classes - used to interact with the database
• DataModel – link to the database (think connection string)
• Classes linked to database tables
▪View
• Structure of the page
▪Controller
• Receives request and calls
resources
Model
▪ Dataand rules applying to the data
▪ Model gives the controller a data representation of what user requested
• Controller will determine what to do with it
▪ Createclass for each of the tables needed in the database
▪ SQL Server Express LocalDB can be created from the classes
5
2024/09/27
Controller
▪ Manages user requests (received as HTTP GET or
POST requests)
▪ Main function is to call and coordinate necessary
resources/objects needed to perform user action
▪ Issues commands to model
▪ Updates the view whenever data changes
▪ Keeps view and model separate
View
▪ Provides different ways to present the data
received from the model
▪ May be templates where data is filled
▪ Could have several different views – controller
will decide which to use
▪ The user interface (HTML/ASP controls)
necessary to render the model to the user
6
2024/09/27
Advantages
▪ Support for different users using different devices
• Interface different but model provides the same data
• Controller will choose which view to use
▪ Separation of concern
• Loosely coupled: should be able to change anything on any 1 of the 3 without affecting the others
▪ Reduces complexity amongst the components
▪ Structured code making it easier to maintain, test and reuse
Quiz 2
▪Loginto Moodle
▪Complete Quiz 2 (available from 08:00)
▪Password: sepTember
7
2024/09/27
▪ Controller:
• Select appropriate view (pc, mobile, etc.)
▪ View
• Present data to user
MVCMovie
▪ Differentstructure visible in
Solution Explorer
• Controllers
• Models
• Views
8
2024/09/27
MVCMovie
▪Indexpage
▪Create Movie page
URL components
▪ Consider the URL:
http://localhost/Home/Privacy
• Where does the last two components come from?
• Home
• Privacy
▪ Home – Controller
▪ Privacy – Name of the method
• Index
method would be called by
default, if no method was specified
9
2024/09/27
10
2024/09/27
11
2024/09/27
12
2024/09/27
13
2024/09/27
14
2024/09/27
15
2024/09/27
Adding Views
▪ When a method is called the controller needs to direct the user to the relevant view
▪ Right-click on the method, Add view
▪ View Templates exist to provide skeleton structure
• Create / Delete / Details / Edit / List / Empty
16
2024/09/27
17
2024/09/27
<Body>
▪ Navigation
▪ The entire body of the
page (view) replaces
@RenderBody()
▪ Footer
18
2024/09/27
Practical 07 requirements
▪ Topic: Your favourite Book Series / Streaming Service Series / Movie Series
▪ Instructions to create the example application is available in MVC Application Tutorial.pdf
• Prac task is basically following those steps for a different application
▪ Create a new ASP.NET Core (MVC) web application with individual accounts authentication:
• Authentication: just select it at the start, nothing else to add for functionality at this stage
▪ Must have a database – single table sufficient
• Table must have at least 4 fields
Practical 07 requirements
▪ 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); add at least 3 records to your
database (using the create option from within your application)
▪ 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
19
2024/09/27
Practical 07 requirements
▪ Create a word document to show that your web application runs, and what the pages
looks like in the browser
• Create a heading for each page in your web application
▪ Viewall the pages in your web application in the browser
▪ Make images/screenshots of each of the pages (you can use a tool like the Snipping tool)
• Paste the image for each page under the relevant heading
▪ Save the word document as a pdf document in the root of your web application
Prac 07 Submission
▪ As the C# ASP.NET Core MVC applications are quite big, you need to remove the
intermediate and output files before uploading to Moodle
▪ Click on Build, Clean Solution
▪ Compress this version, and upload
the compressed folder to Moodle
▪ Due on Wednesday 2 October, by
12:00 (mid-day)
20
2024/09/27
Resources
▪MVC Application Tutorial
▪ASP.NET Core Crash Course – C# App in One Hour
• Video tutorial available on F:\WRWV202
• Video handles more than what is required for prac 7, but some of the other aspects
will be covered in a follow up lecture
▪Why use MVC:
https://www.codeproject.com/Articles/821275/Webforms-vs-MVC-and-
Why-MVC-is-better
21