MVC Example - (ADO.NET Examples)
MVC stands for Model-View-Controller and is a popular architecture pattern used in web development. In a MVC application, the Model represents the data and business logic, the View represents the presentation layer, and the Controller acts as an intermediary between the Model and the View. In this example, we will use ADO.NET, which is a data access technology used by .NET Framework, to implement the Model layer.
Syntax
To create an ADO.NET MVC application, you need to have a basic understanding of ADO.NET. ADO.NET provides a set of classes for accessing and manipulating data from a database. To use ADO.NET, you need to include the System.Data namespace in your code and create a connection to the database using a Connection object.
Example
Here's an example of an MVC application that fetches data from a database using ADO.NET:
// Model layer
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
public class EmployeeContext : DbContext
{
public EmployeeContext() : base("name=EmployeeContext")
{
}
public DbSet<Employee> Employees { get; set; }
}
// Controller layer
public class EmployeeController : Controller
{
public ActionResult Index()
{
var db = new EmployeeContext();
var employees = db.Employees.ToList();
return View(employees);
}
}
// View layer
@model List<Employee>
@foreach (var employee in Model)
{
<div>
<h3>@employee.Name</h3>
<p>@employee.Age</p>
</div>
}
This code defines an Employee model class with properties for Id, Name, and Age. It also defines a DbContext class for managing access to the database.
The Controller class defines an Index() action that fetches a list of employees from the database using the EmployeeContext class and passes the list to a View.
The View is defined using Razor syntax and displays the list of employees.
Output
When you run this application, it displays a list of employees with their names and ages.
Explanation
This ADO.NET MVC example demonstrates how the Model layer can be implemented using ADO.NET. The Model layer represents the data and business logic, in this case the Employee class and the EmployeeContext class for managing access to the database using ADO.NET.
The Controller layer acts as an intermediary between the Model and the View, fetching data from the database and passing it to the View. The View layer represents the presentation layer and displays the data.
Use
ADO.NET is a powerful technology for accessing and manipulating data in a database. In an MVC application, ADO.NET can be used to implement the Model layer, providing access to data and business logic.
Important Points
- ADO.NET is a data access technology used by .NET Framework.
- ADO.NET provides a set of classes for accessing and manipulating data from a database.
- ADO.NET can be used to implement the Model layer in an MVC application.
- An MVC application consists of three layers: Model (data and business logic), View (presentation layer), and Controller (intermediary between Model and View).
Summary
This page covered an ADO.NET MVC example. We discussed the syntax, example, output, explanation, use, and important points of the example. ADO.NET is a powerful data access technology used by .NET Framework, and can be used to implement the Model layer in an MVC application.