Download as pdf or txt
Download as pdf or txt
You are on page 1of 103

NATIONAL ECONOMICS UNIVERSITY

SCHOOL OF INFORMATION TECHNOLOGY AND DIGITAL ECONOMICS

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

MVC CUSTOMER LISTS


OBJECTIVES
 1. Model →

 II. Admin → MVC Model → Admin (CRUD)


 III. Member → Login→ EmailAddress and Password
 → Check DB
 Exists/Not

 IV. Guest→ Registration


 Copy Template from Create.cshtml
 Register → Add new to DB

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

 ASP.NET Core Web App (MVC):


 Similar to the "ASP.NET Core Web App" template, but explicitly emphasizes the use of the Model-View-Controller (MVC) architectural pattern.
 Provides a structured way to build web applications where data, presentation, and logic are separated into models, views, and
controllers.
 Suitable for developers familiar with MVC and who prefer to build applications following this pattern.
 Offers a balanced approach between server-rendered views and API endpoints for data access.
Thaop@neu.edu.vn
CREATE A NEW PROJECT
 Start Visual Studio and select
Create a new project.
 In the Create a new project
dialog, select ASP.NET Core
Web App (Model-View-
Controller) > Next.
 In the Configure your new
project dialog, enter
MvcShopping for Project name.
 Select Next.

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

 Right-click the Models folder > Add >


Class. Name the file Customers.cs.

Thaop@neu.edu.vn
SCAFFOLD CUSTOMER PAGES

 Use the scaffolding


tool to produce
Create, Read,
Update, and Delete
(CRUD) pages for
the movie model.
 In Solution
Explorer, right-
click
the Controllers fol
der and
select Add >
New Scaffolded
Item.

Thaop@neu.edu.vn
SCAFFOLD CUSTOMER PAGES

 In the Add New


Scaffolded Item dialog:
• In the left pane,
select Installed > Com
mon > MVC.
• Select MVC Controller
with views, using
Entity Framework.
• Select Add.

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.

•In the Database provider drop down,


select SQL Server.
•Views and Controller name: Keep the
default.
•Select Add.
Thaop@neu.edu.vn
SCAFFOLD CUSTOMER PAGES

Thaop@neu.edu.vn
SCAFFOLD CUSTOMER PAGES

 Scaffolding adds the following  Scaffolding updates the following:


packages:  Inserts required package references in the
MvcMovie.csproj project file.
 Microsoft.EntityFrameworkCore.SqlServer
 Registers the database context in the Program.cs
 Microsoft.EntityFrameworkCore.Tools file.
 Microsoft.VisualStudio.Web.CodeGeneration.Design  Adds a database connection string to the
appsettings.json file.
 Scaffolding creates the following:
 The automatic creation of these files and file
 A movies controller: updates is known as scaffolding.
Controllers/CustomersController.cs  However, the scaffolded pages can't be used
 Razor view files for Create, Delete, Details, yet because the database doesn't exist.
Edit, and Index pages:  Running the app and selecting the Movie App
Views/Customers/*.cshtml link results in a Cannot open database or no such
table: Movie error message.
 A database context class:  Build the app to verify that there are no errors.
Data/MvcCustomersContext.cs

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

 1. Change Connect string to existing database


 "Data Source = DESKTOP-LEKLOV9\\SQLEXPRESS; Initial Catalog = Store; User ID = sa;
Password = dev; Encrypt=False"
 2. or use Migrations to Create Database

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 .

In the Package Manager Console (PMC), enter the


following commands:
Add-Migration InitialCreate
Update-Database
Thaop@neu.edu.vn
INITIAL MIGRATIONS
 Add-Migration InitialCreate:
 Generates a
Migrations/{timestamp}_InitialCr
eate.cs migration file.
 The InitialCreate argument is the
migration name. Any name can
be used, but by convention, a
name is selected that describes
the migration.
 Because this is the first
migration, the generated class
contains code to create the
database schema.
 The database schema is based
on the model specified in the
MvcCustomerContext class.

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

The InitialCreate class

Thaop@neu.edu.vn
CHECK BY USERNAME PASSWORD

LOGIN PAGE
LOGIN PAGE

 Add New Folder


 Add new View Login

Thaop@neu.edu.vn
LOGIN PAGE

 Add New Folder


 Add new View Login

Thaop@neu.edu.vn
LOGIN PAGE

 Add New Folder


 Add new View Login
 Razor View - Empty

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

 Create a database and tables


by running the script.
 For each table, create an
Admin Page following the
structure outlined in the
diagram.
 Additionally, add a menu to
the Admin Page.

Thaop@neu.edu.vn
PRODUCT SEARCH PAGE
→ ADD TO CART -- > VIEW SHOPPING CART

0. PRODUCT DETAIL PAGE


1. ADD TO CART
2. SHOPPING CART
PRODUCT SEARCH PAGE

ADVANCED SEARCH PRODUCTS


ADVANCED SEARCH PRODUCT

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

0. PRODUCT DETAIL PAGE


1. ADD TO CART
2. SHOPPING CART
THE OBJECTIVES

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

Model Add ShoppingCartItem

View Modify Search View.cshtml

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

0. PRODUCT DETAIL PAGE


1. ADD TO CART
2.VIEW SHOPPING CART
Thaop@neu.edu.vn
Thaop@neu.edu.vn
Thaop@neu.edu.vn
Thaop@neu.edu.vn
PRODUCT SEARCH PAGE → ADD TO CART
-- > VIEW SHOPPING CART → MODIFY SHOPPING CART

0. PRODUCT DETAIL PAGE


1. ADD TO CART
2.VIEW SHOPPING CART
3. UPDATE SHOPPING
CART
Thaop@neu.edu.vn
Thaop@neu.edu.vn
Thaop@neu.edu.vn
Thaop@neu.edu.vn
PRODUCT SEARCH PAGE → ADD TO CART
-- > VIEW SHOPPING CART → MODIFY SHOPPING CART

0. PRODUCT DETAIL PAGE


1. ADD TO CART
2.VIEW SHOPPING CART
3. UPDATE SHOPPING CART

4. CHECK OUT PAGE


CHECKOUT – VIEW

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

0. PRODUCT DETAIL PAGE


1. ADD TO CART
2.VIEW SHOPPING CART
3. UPDATE SHOPPING CART

4. CHECK OUT PAGE


5. ORDER-ORDERDETAILS
 Modify table Orders

Thaop@neu.edu.vn
 Update IDStatus

Thaop@neu.edu.vn
Thaop@neu.edu.vn
Thaop@neu.edu.vn
Thaop@neu.edu.vn
EXERCISE

 Modify Orders Page


 Change Customers and Status with DropDownList
 Show OrderDetails

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

You might also like