Professional Documents
Culture Documents
53 ASPNET Core Web App MVC - P7.8 Login All Search Product Shopping Cart - View Cart_v2 Check Out
53 ASPNET Core Web App MVC - P7.8 Login All Search Product Shopping Cart - View Cart_v2 Check Out
CHAPTER IV
ASP.NET WEB APP (MVC)
PHAM THAO
Thaop@neu.edu.vn
OUTLINE
ASP.NET Core Web App MVC
The Model-View-Controller (MVC) architectural CUSTOMER LISTS ADMIN
Add a controller
USER LOGIN
Change Method
Add a view ADVANCED SEARCH PRODUCTS
Change Layout
Passing Data from the Controller to the View SHOPPING CART
Add a model
Scaffolding movie Pages
VIEW CART
Initial migration
MODIFY CART
Run MovieWebsite
appsettings.json CHECK OUT
Work with a database
Controller actions and views
Add a new field
Add validation
Examine Details and Delete
Thaop@neu.edu.vn
CHECK BY USERNAME PASSWORD
Thaop@neu.edu.vn
THE OBJECTIVES
ASP.NET Core Web App (MVC)
Thaop@neu.edu.vn
SETUP ASP.NET AND WEB DEVELOPMENT
https://learn.microsoft.com/vi-vn/aspnet/core/tutorials/first-mvc-app/start-mvc?view=aspnetcore-7.0&tabs=visual-studio
Thaop@neu.edu.vn
CREATE A NEW PROJECT
Thaop@neu.edu.vn
CREATE A NEW PROJECT
In the Additional information dialog:
Select .NET 7.0.
Verify that Do not use top-level statements is
unchecked.
Select Create.
Thaop@neu.edu.vn
CREATE A NEW PROJECT
Select Ctrl+F5 to run the app without
the debugger.
Visual Studio displays the following
dialog when a project is not yet
configured to use SSL:
Thaop@neu.edu.vn
CREATE A NEW PROJECT
Default
Home
Action: Index
Thaop@neu.edu.vn
ADD A VIEW - CHANGE VIEWS AND LAYOUT PAGES
Default layout
Layout templates allow:
Open the Views/Shared/_Layout.cshtml Specifying the HTML container layout of
file. a site in one place.
Applying the HTML container layout
across multiple pages in the site.
Find the @RenderBody() line.
RenderBody is a placeholder where all
the view-specific pages you create show
up, wrapped in the layout page. For
example, if you select the Privacy link,
the Views/Home/Privacy.cshtml view is
rendered inside the RenderBody
method.
Change share layout
Thaop@neu.edu.vn
ADD MODEL
Right-click the Models folder > Add > Class. Name the file Customers.cs.
Thaop@neu.edu.vn
ADD MODEL
Thaop@neu.edu.vn
SCAFFOLD CUSTOMER PAGES
Thaop@neu.edu.vn
SCAFFOLD CUSTOMER PAGES
Thaop@neu.edu.vn
SCAFFOLD CUSTOMER PAGES
Complete the Add MVC
Controller with views, using
Entity Framework dialog:
• In the Model class drop down,
select Movie
(MvcMovie.Models).
• In the Data context class row,
select the + (plus) sign.
• In the Add Data Context dialog,
the class
name MvcMovie.Data.MvcMovieC
ontext is generated.
• Select Add.
Thaop@neu.edu.vn
SCAFFOLD CUSTOMER PAGES
Thaop@neu.edu.vn
SCAFFOLD CUSTOMER PAGES
Thaop@neu.edu.vn
SCAFFOLD MOVIE PAGES
Thaop@neu.edu.vn
SCAFFOLD MOVIE PAGES
Thaop@neu.edu.vn
TEST
1. Change
Connect
string to
existing
database or
2. use
Migration to
Create
Database
Thaop@neu.edu.vn
TEST
Thaop@neu.edu.vn
Thaop@neu.edu.vn
INITIAL MIGRATIONS
Use the EF
Core Migrations feature to
create the
database. Migrations is a
set of tools that create
and update a database to
match the data model.
From the Tools menu,
select NuGet Package
Manager > Package
Manager Console .
Thaop@neu.edu.vn
INITIAL MIGRATIONS
Update-Database:
Updates the database to the latest migration, which the
previous command created.
This command runs the Up method in the Migrations/{time-
stamp}_InitialCreate.cs file, which creates the database.
Thaop@neu.edu.vn
INITIAL MIGRATIONS
Update-Database:
Updates the database to the latest
migration, which the previous command
created.
This command runs the Up method in the
Migrations/{time-stamp}_InitialCreate.cs
file, which creates the database.
Thaop@neu.edu.vn
INITIAL MIGRATIONS
Thaop@neu.edu.vn
ADD A ‘CUSTOMERS' MENU
Thaop@neu.edu.vn
ADD A ‘CUSTOMERS' MENU
Thaop@neu.edu.vn
CHECK THE CONFIGURATION
appsettings.json
Thaop@neu.edu.vn
CHECK THE CONFIGURATION
Thaop@neu.edu.vn
CHECK BY USERNAME PASSWORD
LOGIN PAGE
LOGIN PAGE
Thaop@neu.edu.vn
LOGIN PAGE
Thaop@neu.edu.vn
LOGIN PAGE
Thaop@neu.edu.vn
LOGIN PAGE
Thaop@neu.edu.vn
LOGIN PAGE
Add Customer
Controller to Controller
Folder
Thaop@neu.edu.vn
LOGIN PAGE
Thaop@neu.edu.vn
LOGIN PAGE
Modify IActionResult
Add ActionResult
Index (Model)
Thaop@neu.edu.vn
LOGIN PAGE
Modify IActionResult
Add ActionResult Index (Model)
Add IActionResult Index
(Model)
Thaop@neu.edu.vn
LOGIN PAGE
Thaop@neu.edu.vn
ADD WELCOME VIEW
Thaop@neu.edu.vn
EXERCISE
Thaop@neu.edu.vn
PRODUCT SEARCH PAGE
→ ADD TO CART -- > VIEW SHOPPING CART
Thaop@neu.edu.vn
ADVANCED SEARCH PRODUCTS
Add Menu to (Share→_Layout.cshtml)
Thaop@neu.edu.vn
ADVANCED SEARCH PRODUCTS - VIEW
Add Search.cshtml in
Views/Products
Thaop@neu.edu.vn
ADVANCED SEARCH PRODUCTS - MODEL
Add ProductSearchViewModel.cs in
Models
Thaop@neu.edu.vn
ADVANCED SEARCH PRODUCTS - MODEL
Modify MVCShoppingContext.cs
Thaop@neu.edu.vn
ADVANCED SEARCH PRODUCTS - MODEL
Modify MVCShoppingContext.cs
Thaop@neu.edu.vn
ADVANCED SEARCH PRODUCTS - CONTROLLER
Add ProductsController.cs
Thaop@neu.edu.vn
ADVANCED SEARCH PRODUCTS - CONTROLLER
Add ProductsController.cs
Thaop@neu.edu.vn
ADVANCED SEARCH PRODUCTS - RESULTS
Thaop@neu.edu.vn
PRODUCT SEARCH PAGE → ADD TO CART -- > VIEW SHOPPING CART
1. Form 2. Model Binding: 3. Product 4. Cart Retrieval: 5. Cart Update: 6. Session Update:
Submission: Retrieval:
When the “Add to Cart” When the server receives The AddToCart action Next, it attempts to Then, it checks if the Finally, it saves the updated
button is clicked on the the HTTP POST request, it begins by attempting to retrieve the shopping cart product is already in the cart back to the session and
form, the form data is uses model binding to take retrieve the product with from the session. If a cart cart. If not, it adds a new redirects back to the index
packaged into an HTTP the raw form data and the given productId from doesn’t exist, it creates a item; otherwise, it page.
POST request and sent to convert it into .NET types the database. new one. increments the quantity of
the server. for the AddToCart action the existing item.
The asp- parameters.
action="AddToCart" The name attributes in your
attribute in the form tag input fields
tells the server to route this (name="productId" and
request to the AddToCart name="quantity") match up
action in your controller. with the parameter names
in your AddToCart action
(int productId, int quantity).
Thaop@neu.edu.vn
STEPS
Controller ShoppingCartController
Thaop@neu.edu.vn
SHOPPINGCART - VIEW
Modify Search View.cshtml
Thaop@neu.edu.vn
SHOPPINGCART - CONTROLLER
Thaop@neu.edu.vn
SHOPPINGCART - CONTROLLER
Thaop@neu.edu.vn
SHOPPINGCART - CONTROLLER
Thaop@neu.edu.vn
SHOPPINGCART – CONTROLLER - VIEW
Thaop@neu.edu.vn
SHOPPINGCART – CONTROLLER - VIEW
Thaop@neu.edu.vn
SHOPPINGCART – CONTROLLER - VIEW
Thaop@neu.edu.vn
PRODUCT SEARCH PAGE → ADD TO CART
-- > VIEW SHOPPING CART
Thaop@neu.edu.vn
CHECKOUT – MODELS
Thaop@neu.edu.vn
CHECKOUT – MODELS
Thaop@neu.edu.vn
CHECKOUT – RUN-TEST
Check Database
Thaop@neu.edu.vn
PRODUCT SEARCH PAGE → ADD TO CART
-- > VIEW SHOPPING CART → MODIFY SHOPPING CART
Thaop@neu.edu.vn
Update IDStatus
Thaop@neu.edu.vn
Thaop@neu.edu.vn
Thaop@neu.edu.vn
Thaop@neu.edu.vn
EXERCISE
Thaop@neu.edu.vn
Thaop@neu.edu.vn
Thaop@neu.edu.vn
Thaop@neu.edu.vn
Thaop@neu.edu.vn
Thaop@neu.edu.vn
Thaop@neu.edu.vn
Thaop@neu.edu.vn
Thaop@neu.edu.vn
Thaop@neu.edu.vn
Thaop@neu.edu.vn
Thaop@neu.edu.vn
Thaop@neu.edu.vn
Thaop@neu.edu.vn
Thaop@neu.edu.vn