Back

Introduction to the Model-View-Controller (MVC) pattern in .NET

Introduction to the Model-View-Controller (MVC) pattern in .NET

The Model-View-Controller (MVC) pattern is a widely used architectural pattern for building web applications. It separates the application logic into three distinct components: the model, the view, and the controller.

The Model-View-Controller (MVC) pattern is a widely used architectural pattern for building web applications. It separates the application logic into three distinct components: the model, the view, and the controller.

The model represents the data and the business logic of the application. It is responsible for retrieving and storing data, and for performing any necessary calculations or operations on that data. In .NET, the model is typically represented by a class or set of classes, and can be built using any number of data access technologies such as Entity Framework, ADO.NET, or even plain old ADO.

Here's an example of a simple model class in .NET:

public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public int Quantity { get; set; }
public decimal CalculateTotal()
{
return Price * Quantity;
}
}

In this example, the Product class represents a product with an ID, name, price, and quantity. It also includes a method CalculateTotal() that calculates the total cost of the product.

The view is responsible for displaying the data to the user. It is typically written in HTML and may include elements such as text boxes, labels, and buttons. In .NET, views are typically created using Razor, a markup language that allows you to embed C# code within HTML.

Here's an example of a simple view that displays a list of products:

<table>
<thead>
<tr>
<th>Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Total</th>
</tr>
</thead>
<tbody>
@foreach (var product in Model)
{
<tr>
<td>@product.Name</td>
<td>@product.Price</td>
<td>@product.Quantity</td>
<td>@product.CalculateTotal()</td>
</tr>
}
</tbody>
</table>

In this example, the view uses a foreach loop to iterate through a collection of products passed to it by the controller. It then displays the name, price, quantity, and total cost of each product in a table.

The controller is responsible for handling user input and updating the model and the view accordingly. It receives requests from the user, retrieves or updates data in the model, and returns the appropriate view to the user. In .NET, controllers are typically created using C# classes that inherit from the Controller base class.

Here's an example of a simple controller that handles requests to display a list of products:

public class ProductController : Controller
{
private readonly IProductRepository _repository;
public ProductController(IProductRepository repository)
{
_repository = repository;
}
public IActionResult Index()
{
var products = _repository.GetAll();
return View(products);
}
}

In this example, the ProductController class has a single action method `Index()