Skip to content
  • iImagine
  • Register
  • Log In

Web Development School

Learning made easy.

  • Books
    • Beginning Web Development with ASP.Net Core & Client-Side Technologies
      • TOC
      • Part 1
        • Chapter 1: Static HTML – Designing the landing page
      • Part 2
        • Chapter 2: ASP.Net Core – Let’s talk Dynamic
        • Chapter 3: Introduction to ASP.Net Core MVC
          [ASP.Net Core v9]
      • Part 4
        • Chapter 7: Using Server Side & Client Side technologies together
          [ASP.Net Core v7 & Angular 15]
  • Environment Setup
    • Installing Angular
    • Installing Visual Studio 2022
    • Installing SQL Server 2022 Express
    • Installing Postman
    • Installing Git for Windows
  • Blog
  • iImagine WebSolutions
  • Events
  • Learning Videos
  • Toggle search form

Add the ImagePath Migration and Thumbnail images to results

In this module we are going add thumbnail images to the Vehicle rows in the results table.

Table Of Contents
  1. Preparing for the Module
  2. Add the images to the wwwroot folder
  3. Remove the VIN column
  4. Add the ImagePath property to the Vehicle model
  5. Add the ImagePath Migration
  6. Modify the SeedData class
  7. Comparing Dynamic Results to Static HTML
  8. What's Next

Preparing for the Module

The first thing we’ll need to do is place the vehicle images somewhere under the wwwroot folder. So, download the zip file containing the images from the following link to your downloads folder.

Download images

Add the images to the wwwroot folder

Create a new folder under the wwwroot folder called images. Extract the zip file Chapt03-Mod16-vehicles-images.zip in your downloads folder.

Double click on the extracted file named Chapt03-Mod16-vehicles-images (without the .zip extension) in your downloads file to open it. You may have to double click on another subfolder with the same name to get to the contents of the extracted file. Next, copy the three folders named cars, trucks, and jeeps from the extracted contents in the downloads folder to the newly created images folder under the wwwroot folder in the project using windows explorer

The vehicle images should now show up in Solution Explorer under the wwwroot/images folder.

Remove the VIN column

Currently, when we run the application, the first column in the results table is the Vehicle’s VIN number. This isn’t a piece of information a user is likely to find important when searching for a vehicle to buy. The VIN number will be much more useful on the details page we are going to build in a later module. So, we are going to replace that column with a thumbnail image of each vehicle which will be much more useful and a lot more fun to look at.

Add the ImagePath property to the Vehicle model

To show thumbnail images for vehicle’s, each Vehicle entity will have to know where its image is stored. Add the ImagePath property to the Vehicle class to store this information with the modified code shown below.

FredsCars\Models\Vehicle.cs

... existing code ...
public class Vehicle
{
    public int Id { get; set; }
    public Status Status { get; set; }
    public string Year { get; set; } = string.Empty;
    public string Make { get; set; } = string.Empty;
    public string Model { get; set; } = string.Empty;
    public string Color {  get; set; } = string.Empty;
    [Column(TypeName = "decimal(9, 2)")]
    [DataType(DataType.Currency)]
    public decimal Price { get; set; }
    public string VIN { get; set; } = string.Empty;
    public string? ImagePath { get; set; } = string.Empty;
... existing code ...

In the above code we make the new ImagePath property nullable just in case no image is provided.

Add the ImagePath Migration

Since we have modified the model, we need to create a migration and update the database so that it will reflect the model’s changes.

Open up a command window and navigate to the FredsCars project folder (the one with the fredscars.csproj file in it) and run the following commands.

 dotnet ef migrations add ImagePath
 dotnet ef database update

The new ImagePath property should now show up in the database in the Vehicle table.

Modify the SeedData class

The next thing we need to do is modify the seeding routing to populate the ImagePath property for all of our initial test data.

Modify the SeedData class with the code shown below.

FredsCars\Models\SeedData.cs

... existing code ...
// Add Vehicle records
//   if no Vehicle data exists yet.
if (!context.Vehicles.Any())
{
    // Reset Identity seed value to 1
    context.Database
        .ExecuteSqlRaw("DBCC CHECKIDENT ('Vehicle', RESEED, 0)");

    context.Vehicles.AddRange(
        // Cars
        new Vehicle
        {
            Status = Status.New,
            Year = "2021",
            Make = "Dodge",
            Model = "Challenger",
            Color = "Frostbite",
            Price = 64164,
            VIN = "2C3CDZFJ8MH631199",
            VehicleTypeId = carTypeId,
            ImagePath = "/images/cars/car1.webp"
        },
        new Vehicle
        {
            Status = Status.Used,
            Year = "2020",
            Make = "Ford",
            Model = "Escape",
            Color = "Oxford White",
            Price = 22999,
            VIN = "1FMCU0F63LUC25826",
            VehicleTypeId = carTypeId,
            ImagePath = "/images/cars/car2.webp"
        },
        new Vehicle
        {
            Status = Status.New,
            Year = "2021",
            Make = "Dodge",
            Model = "Durange",
            Color = "Black",
            Price = 50557,
            VIN = "1C4RDJDG5MC837730",
            VehicleTypeId = carTypeId,
            ImagePath = "/images/cars/car3.webp"
        },
        new Vehicle
        {
            Status = Status.New,
            Year = "2021",
            Make = "Nissan",
            Model = "Niro",
            Color = "Blue",
            Price = 24960,
            VIN = "2XYZT67JTF24AZG856",
            VehicleTypeId = carTypeId,
            ImagePath = "/images/cars/Car4.png"
        },
        new Vehicle
        {
            Status = Status.New,
            Year = "2021",
            Make = "Kia",
            Model = "Stinger",
            Color = "Gray",
            Price = 36090,
            VIN = "6FG146B89624AZ7952",
            VehicleTypeId = carTypeId,
            ImagePath = "/images/cars/Car5.png"
        },
        new Vehicle
        {
            Status = Status.New,
            Year = "2021",
            Make = "Kia",
            Model = "Stinger",
            Color = "Gray",
            Price = 36090,
            VIN = "6FG146B89624AZ7952",
            VehicleTypeId = carTypeId,
            ImagePath = "/images/cars/Car6.png"
        },
        // Trucks
        new Vehicle
        {
            Status = Status.New,
            Year = "2022",
            Make = "Ram",
            Model = "Crew Cab",
            Color = "Black",
            Price = 68400,
            VIN = "3C6UR5DL8NG157035",
            VehicleTypeId = truckTypeId,
            ImagePath = "/images/trucks/truck1.webp"
        },
        new Vehicle
        {
            Status = Status.Used,
            Year = "2017",
            Make = "Ram",
            Model = "Crew Cab",
            Color = "Red",
            Price = 33000,
            VIN = "1C6RR7PT0HS814596",
            VehicleTypeId = truckTypeId,
            ImagePath = "/images/trucks/truck2.webp"
        },
        // Jeeps
        new Vehicle
        {
            Status = Status.New,
            Year = "2022",
            Make = "Jeep",
            Model = "Compass",
            Color = "White",
            Price = 34980,
            VIN = "3C4NJDFB5NT114024",
            VehicleTypeId = jeepTypeId,
            ImagePath = "/images/jeeps/jeep1.webp"
        },
        new Vehicle
        {
            Status = Status.New,
            Year = "2022",
            Make = "Jeep",
            Model = "Compass",
            Color = "Red",
            Price = 39275,
            VIN = "3C4NJDCB1NT118172",
            VehicleTypeId = jeepTypeId,
            ImagePath = "/images/jeeps/jeep2.webp"
        },
        new Vehicle
        {
            Status = Status.New,
            Year = "2022",
            Make = "Jeep",
            Model = "Grand Cherokee",
            Color = "Pearlcoat",
            Price = 53575,
            VIN = "1C4RJKBG5M8201121",
            VehicleTypeId = jeepTypeId,
            ImagePath = "/images/jeeps/jeep3.webp"
        },
        new Vehicle
        {
            Status = Status.New,
            Year = "2021",
            Make = "Jeep",
            Model = "Wrangler Sport S",
            Color = "Green",
            Price = 40940,
            VIN = "1C4GJXAN0MW856433",
            VehicleTypeId = jeepTypeId,
            ImagePath = "/images/jeeps/jeep4.webp"
        }
    );
    context.SaveChanges();
... existing code ...

Delete the Vehicle data from the Vehicle table using SSOX (SQL Server Object Explorer) in Visual Studio and restart the application. The seeding routine will then reseed the Vehicle table with our test data this time including the ImagePath.

Once you restart the application and navigate to it in a browser you should see results similar to the screenshot shown below.

Now the thumbnail images show as the first column in the results table. This is much more nicer to look at then a VIN number don’t you think?

Comparing Dynamic Results to Static HTML

Now we have completed the list feature. But, I hope you haven’t forgotten how in chapter one we had to painstakingly craft the HTML for each Vehicle. Here in our first dynamic framework, MVC, all an administrator would have to do is enter another row of vehicle data into the database and that new Vehicle would show up in the results. We would not have to touch the HTML or modify any component’s C# code.

What’s Next

Now that we have completed the list feature, it would be nice to let the user sort the data and page through the results. In the next module we will impliment the paging and sorting features.

< Prev
Next >

Leave a ReplyCancel reply

Chapter 1: Static HTML – Designing the landing page.

  • Static HTML – Designing the landing page.
  • Let’s get started!
  • Mock your site with HTML
  • Make CSS easy with Bootstrap
  • Mock your content
  • Introducing JavaScript
  • JavaScript Code Improvements
  • Results Data
  • Images and the HTML Image Element.
  • Revisiting Reusability for CSS and JavaScript
  • Reuse for HTML: PART 1
  • Reuse for HTML: PART 2
  • Details Page – Using a Bootstrap Component
  • Creating Links
  • Chapter One Conclusion

Chapter 2: ASP.Net Core – Let’s talk Dynamic

  • Introduction to ASP.Net Core
  • What is .Net?
  • What is ASP.Net
  • Introduction to Entity Framework Core

Chapter 3: ASP.Net MVC Core – Models, Views, and Controllers [ASP.Net Core v9]

  • Introduction to ASP.Net Core MVC
  • Create the project: ASP.Net Core MVC
  • Explore the ASP.Net Core Empty Web Project Template
  • Configure the Application for MVC
  • Create a Controller: Home Controller
  • Create a View: Index View for the Home Controller
  • Install Bootstrap using Libman
  • Create the Layout template
  • Create the Model
  • Install EF Core & Create the Database
  • Seed the Database: Loading test data
  • DI (Dependency Injection): Display a List of Vehicles
  • Repository Pattern: The Vehicles Repo
  • Unit Test 1: Home Controller Can Use Vehicle Repository
  • Unit Test 2: Vehicle Repository Can Return List
  • Add the ImagePath Migration and Thumbnail images to results
  • Pagination: Create a Custom Tag Helper
  • Sorting
  • Category Filter
  • Partial View: Break out the vehicle results
  • View Component: Create dynamic category buttons
  • Create the Details page
  • Create the Create Page

Chapter 7: Using Server Side & Client Side technologies together. [ASP.Net Core v7 & Angular v15]

  • Intro to Full Stack Development
  • Fred’s Cars – Full Stack Development
  • Prepare the environment
  • Create the Visual Studio Solution
  • Add the ASP.Net Core Web API project
  • Add the Angular Project
  • Wire it up!
  • WeatherForecast: Understanding the basics
  • Vehicles API Controller: Mock Data
  • Vehicles Angular Component: Consuming Data
  • Routing and Navigation
  • Using a Component Library: Angular Material
  • Our first Angular Material Component: MatToolbar
  • Configuring for Saas: CSS with superpowers
  • Create the Header & Footer components
  • Displaying Results with MatTable
  • Loading: Using a Progress Spinner
  • MatTable: Client-Side Paging and Sorting
  • MatSidenav: Create a Search Sidebar
  • MatCheckbox: Category Search UI
  • Adding an image to the welcome page
  • Create the database with Entity Framework Core migrations
  • MatPaginator & PageEvent: Custom Server-Side Paging
  • Unit Testing: Custom Server-Side Paging
  • Repository Pattern: VehicleRepository
  • Unit Test: Paging in the Vehicles controller
  • Server-Side Sorting
  • Unit Tests: Sorting
  • Filter (Quick Search)
  • Unit Tests: Filter feature
  • Advanced Search: Categories
  • Unit Tests: Search by category
  • Progress Spinner: Final Fix

TOC

  • What were WebForms?
  • Enter MVC
    • Understanding MVC
    • Advantages of MVC
  • ASP.Net Core MVC – A total rewrite
  • ASP.Net Core 2 MVC – Here come Razor Pages
    • Understanding Razor Pages
  • ASP.Net Core 3 – Dropping the MVC reference
    • Understanding Blazor
  • Dropping the MVC reference
  • Hello .Net 5!
  • What’s Next? – Here comes .Net 6.

Recent Posts

  • Angular Commands Cheat Sheet
  • Installing Git for Windows
  • Installing Postman
  • Installing SQL Server 2022 Express
  • Installing Visual Studio 2022

Recent Comments

No comments to show.

Archives

  • November 2023
  • October 2023
  • June 2023
  • October 2021

Categories

  • Angular
  • ASP.Net
  • Environment Setup
  • See All
  • SQL Server
  • Visual Studio
  • Web API & Rest Services

WordPress Theme Editor

Copyright © 2025 Web Development School.

Powered by PressBook Blog WordPress theme