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, and wwwroot. Understand the role of Program.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.
  • Actions in Controllers:

    • Return types like ViewResult, JsonResult, ContentResult
    • Handle multiple HTTP methods (GET, POST, etc.)
  • ๐Ÿ”ฒ How to Set Default Page in ASP.NET Core
    (To do: Discuss how to change the default route in Program.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 a Product
  • @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 the Model

๐Ÿงช 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: