Introduction to MVC
Model-View-Controller (MVC) is a software architectural pattern that separates an application into three main logical components:
Model
- Represents the data and business logic
- Handles data validation
- Manages the application state
public class Product
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
public decimal Price { get; set; }
public string Description { get; set; }
}
View
- Presents data to users
- Handles the UI layout
- Uses Razor syntax
@model Product
<h1>@Model.Name</h1>
<div class="product-details">
<p>Price: $@Model.Price</p>
<p>@Model.Description</p>
</div>
Controller
- Processes incoming requests
- Handles user input
- Works with the Model
public class ProductController : Controller
{
private readonly IProductService _service;
public ProductController(IProductService service)
{
_service = service;
}
public IActionResult Details(int id)
{
var product = _service.GetProduct(id);
return View(product);
}
}
Request Flow
- User makes a request to
/Product/Details/1
- Routing system directs to
ProductController
Details
action method is called- Model data is retrieved
- View is rendered with model data
- Response is sent to user