0% found this document useful (0 votes)
15 views

MVC Basic Application Code

The document contains code for an ASP.NET Core MVC web application that implements controllers and models for a guitar shop. It includes a HomeController with Index and About action methods that return views. A ProductController handles product listing and detail pages, using a DB class to retrieve product data from a mock database. Models include classes for Product and DB that represent products and the database interface. Views are defined to display the home, about, product list and detail pages.

Uploaded by

Maryam Waleed
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

MVC Basic Application Code

The document contains code for an ASP.NET Core MVC web application that implements controllers and models for a guitar shop. It includes a HomeController with Index and About action methods that return views. A ProductController handles product listing and detail pages, using a DB class to retrieve product data from a mock database. Models include classes for Product and DB that represent products and the database interface. Views are defined to display the home, about, product list and detail pages.

Uploaded by

Maryam Waleed
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

HOME CONTROLLER

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();
}

// This action method handles requests to the "/Home/About" URL.


public IActionResult About()
{
// Return a view named "About." ASP.NET Core will look for "About.cshtml" in the
"Views/Home" folder.
return View();
}
}
}
//the code of the home controller, it has 2 actions methods, that return just the views

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;
}

// This method retrieves a Product object based on a given slug (identifier).


public static Product GetProduct(string slug)
{
// Retrieve the list of products using the GetProducts method.
List<Product> products = DB.GetProducts();

// Iterate through the list of products.


foreach (Product p in products)
{
// Check if the slug of the current product matches the provided slug.
if (p.Slug == slug)
{
// If a match is found, return the matching Product object.
return p;
}
}
// If no match is found, return null.
return null;
}
}
}

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; }

// The Name property stores the name or title of the product.


public string Name { get; set; }

// The Price property stores the price of the product.


public decimal Price { get; set; }

// The Slug property generates a URL-friendly version of the product's name.


// For example, if the product name is "Fender Stratocaster," Slug becomes "Fender-
Stratocaster."
// It's generated using the C# expression-bodied property syntax.
public string Slug => Name.Replace(' ', '-');
}
}

Home’s about view:


@{
ViewBag.Title = "About";
}
<h1>About Us</h1>
<p>Whenever you buy an instrument from us, we want you to be confident that it's going to be
the right instrument for you. That's been our primary goal throughout our history. That's why
we only sell the highest quality guitars, basses, and drums.</p>
<a asp-action="Index" class="btn btn-primary">Home</a>
Home Index VIEW:
@{
ViewBag.Title = "Home";
}
<h1>Home</h1>
<div class="list-group">
<a asp-controller="Product" asp-action="Detail"
asp-route-id="Fender-Stratocaster">
View Fender Stratocaster
</a>
<a asp-controller="Product" asp-action="List">
View Products
</a>
<a asp-controller="Home" asp-action="About">
About Us </a>. </div>
PRODUCTS DETAILS VIEW
@model Product
@{
ViewBag.Title = "Product Detail";
}
<h1>Product Detail</h1>
<table class="table table-bordered table-striped">
<tr>
<td>ID</td>
<td>@Model.ProductID</td>
</tr>
<tr>
<td>Name</td>
<td>@Model.Name</td>
</tr>
<tr>
<td>Price</td>
<td>@Model.Price.ToString("C2")</td>
</tr>
</table>
<a asp-controller="Home" asp-action="Index"
class="btn btn-primary">Home</a>
PRODUCT LIST VIEW:
@model List<Product>
@{
ViewBag.Title = "Product List";
}
<h1>Product List</h1>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Name</th>
<th>Price</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var product in @Model)
{
<tr>
<td>@product.Name</td>
<td>@($"{product.Price:C}")</td>
<td>
<a asp-controller="Product" asp-action="Detail"
asp-route-id="@product.Slug">View</a>
</td>
</tr>
}
</tbody>
</table>
<a asp-controller="Home" asp-action="Index"
class="btn btn-primary">Home</a>

You might also like