Session 1: Working with Models and MVC Basics in ASP.NET Core
๐ Overview
In this session, weโll cover the following concepts:
- MVC project structure and configuration
- Controllers and routing (including static segments and default page setting)
- Introduction to Models in ASP.NET Core
- C# OOP essentials: properties, encapsulation, and access modifiers
- Passing models (single and list) from controllers to views
- Strongly-typed Razor views
๐ Topics Covered
โ MVC Project Structure & Routing
Learn how ASP.NET Core organizes files and configures routes for handling web requests.
๐ Microsoft Docs - MVC Introduction
โ C# Properties & OOP Essentials
Explore object-oriented programming basics in C#, including properties and encapsulation.
๐ C# OOP Overview
โ Models and Views
Understand how to define models and pass them to views in ASP.NET Core MVC.
๐ Microsoft Docs - Work with Data in MVC
๐ Notes
Collected from various sources including W3Schools, Microsoft Docs, and ChatGPT
๐ Part 0: Roadmap of This Session
โ Getting Started with MVC Projects
-
Directory Structure:
Learn about the standard folders:Controllers
,Views
,Models
, andwwwroot
. Understand the role ofProgram.cs
in bootstrapping and routing. -
Routing and Controllers:
- Custom routing using templates like
{controller}/{action}/{id?}
- Attribute routing using
[Route]
,[HttpGet]
, etc. - Static segments in routes:
Example:[Route("products/all")]
creates/products/all
.
- Custom routing using templates like
-
Actions in Controllers:
- Return types like
ViewResult
,JsonResult
,ContentResult
- Handle multiple HTTP methods (GET, POST, etc.)
- Return types like
-
๐ฒ How to Set Default Page in ASP.NET Core
(To do: Discuss how to change the default route inProgram.cs
, e.g. set controller = Products, action = List)
๐ Part 1: Understanding Models in ASP.NET Core MVC
Models are a fundamental part of the MVC architecture, representing the data structure and logic of your application. They interact with the database and contain properties that hold data and methods that implement business logic.
๐ง Model Structure and Purpose
- Data Representation: Models reflect real-world data structures, often aligning with database tables.
- Data Handling: Models encapsulate validation, relationships, and business logic.
- Data Transport: Used to transfer data between controllers and views.
In ASP.NET Core MVC, models are typically stored in a Models
folder, with one class per entity (Product
, Customer
, Order
, etc.).
๐ Part 2: Properties in Models & C# OOP Concepts
โ Properties in C#
Properties in C# provide a controlled way to access and modify private fields using get
and set
.
public class Product {
public int Id { get; set; } // Auto-property
public decimal Price { get; set; }
}
- **Auto-Implemented Properties**:
Simplify property declarations when no extra logic is needed.
#### โ
Encapsulation and Access Modifiers
Encapsulation hides the internal workings of a class from the outside world.
- **Access Modifiers**:
- `public`: Accessible from anywhere
- `private`: Only inside the class
- `protected`: Inside the class and derived classes
- `internal`: Only within the current assembly
**MVC models typically use public properties** so they can be accessed in views and controllers.
---
### ๐ Part 3: Passing a Model to a View
#### โ
Step 1: Define the Model
```csharp
public class Product {
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
โ Step 2: Create a Controller Action
public class ProductsController : Controller {
public IActionResult Details() {
var product = new Product {
Id = 1,
Name = "Laptop",
Price = 1500.00m
};
return View(product); // Passing model to view
}
}
โ
Step 3: Strongly-Typed View (Details.cshtml
)
@model Product
<h2>Product Details</h2>
<p>Product Name: @Model.Name</p>
<p>Price: @Model.Price</p>
@model
tells Razor this view receives aProduct
@Model
gives access to passed data
๐ Passing a List of Models to the View
โ Controller Action:
public IActionResult List() {
var products = new List<Product> {
new Product { Id = 1, Name = "Laptop", Price = 1500.00m },
new Product { Id = 2, Name = "Smartphone", Price = 800.00m }
};
return View(products);
}
โ
View (List.cshtml
):
@model IEnumerable<Product>
<h2>Product List</h2>
<ul>
@foreach (var product in Model) {
<li>@product.Name - @product.Price</li>
}
</ul>
- Use
IEnumerable<Product>
to pass lists - Razor supports
foreach
directly on theModel
๐งช Practice
- Explore and explain the MVC folder structure
- Create a model class
Product
- Build a controller that returns a single model to a view
- Create a strongly-typed Razor view using
@model
- Return a list of
Product
models and display them in a loop - Show how to set the default controller and action in
Program.cs
๐ Acknowledgments
Sources:
- w3schools.com
- Microsoft Learn
- ChatGPT sessions (2025)