MVC (Model-View-Controller) is a popular design pattern used in software development to separate an application into three interconnected components:
MVC offers several benefits, including:
In .NET, you can use the ASP.NET Core MVC framework to implement the MVC design pattern. Here's a quick example:
// Models/Product.cs
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
The Product
model represents the data structure.
// Controllers/ProductController.cs
using Microsoft.AspNetCore.Mvc;
public class ProductController : Controller
{
public IActionResult Index()
{
var products = new List<Product>
{
new Product { Id = 1, Name = "Laptop", Price = 1200.99m },
new Product { Id = 2, Name = "Mouse", Price = 25.50m }
};
return View(products);
}
}
The ProductController
fetches the data and sends it to the View.
// Views/Product/Index.cshtml
Product List
Id
Name
Price
1
Product 1
10.0
2
Product 2
20.0
3
Product 3
30.0
The Index.cshtml
view displays the list of products in a table format.