MVC Basic Application Code
MVC Basic Application Code
using Microsoft.AspNetCore.Mvc;
namespace GuitarShop.Controllers
{
public class HomeController : Controller
{
// This action method handles requests to the root URL (e.g., "/").
public IActionResult Index()
{
// Return a view named "Index." ASP.NET Core will look for "Index.cshtml" in the
"Views/Home" folder.
return View();
}
PRODUCT CONTROLLER
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using GuitarShop.Models;
namespace GuitarShop.Controllers
{
public class ProductController : Controller
{
public IActionResult Detail(string id)
{
Product product = DB.GetProduct(id);
return View(product);
}
public IActionResult List()
{
List<Product> products = DB.GetProducts();
return View(products);
}
}
}
MODELS: DB
using System;
using System.Collections.Generic;
namespace GuitarShop.Models
{
// The DB class represents a simplified database or data access layer for managing products.
public class DB
{
// This method returns a list of Product objects representing available products.
// GetProducts(), creates a simple list containing 10 Product
public static List<Product> GetProducts()
{
// Create a new list to store Product objects.
List<Product> products = new List<Product>
{
// Product entry 1: Fender Stratocaster with a price of $699.00 (as a decimal).
new Product
{
ProductID = 1,
Name = "Fender Stratocaster",
Price = (decimal)699.00
},
// Product entry 2: Gibson Les Paul with a price of $1199.00 (as a decimal).
new Product
{
ProductID = 2,
Name = "Gibson Les Paul",
Price = (decimal)1199.00
},
// Product entry 3: Gibson SG with a price of $2517.00 (as a decimal).
new Product
{
ProductID = 3,
Name = "Gibson SG",
Price = (decimal)2517.00
};
// Return the list of products.
return products;
}
MODEL PRODUCT:
namespace GuitarShop.Models
{
// The Product class represents individual products in your application.
public class Product
{
// The ProductID property stores a unique identifier for each product.
public int ProductID { get; set; }