---
title: "View data from database using MVC & Entity Framework"  
description: "View data from database using MVC & Entity Framework"  
author: "Utpal Vishwas"  
published: 2023-09-05  
updated: 2023-09-06  
canonical: https://www.mindstick.com/forum/159852/view-data-from-database-using-mvc-entity-framework  
category: "asp.net mvc"  
tags: ["asp.net mvc", "entity framework", "asp.net core"]  
reading_time: 2 minutes  

---

# View data from database using MVC & Entity Framework

[View](https://yourviews.mindstick.com/view/84701/layoffs-in-google-india-2023-view) [data from database](https://www.mindstick.com/forum/34370/how-to-populate-google-charts-using-data-from-database-created-via-data-first-asp-dot-net-mvc) using [MVC](https://www.mindstick.com/forum/155803/define-cache-profile-in-mvc) & [Entity Framework](https://www.mindstick.com/articles/1566/crud-operations-using-entity-framework-code-first-approach)

## Replies

### Reply by Aryan Kumar

Here are the steps display [data](https://www.mindstick.com/articles/13050/salesforce-aiming-to-dominate-predictive-analytics-with-data-science) from the [database](https://www.mindstick.com/articles/12226/use-of-database-in-sencha-extjs-and-insert-record-from-user-form-using-ajax) using MVC and [Entity](https://www.mindstick.com/forum/160562/database-connectivity-using-entity-framework-database-first-approach) [Framework](https://www.mindstick.com/forum/34615/software-framework-vs-library):

1. Create the Entity Framework data model. This can be done using the Entity Framework Designer or by creating models from the database.
2. Create a DbContext class that inherits from `DbContext`. This class will be used to connect to the database and manage the entities.
3. Create a controller that will display the data to the user. The controller will need to use the DbContext class to query the database and return the results to the user.
4. Create a view that will show the data to the user. Views can use a templating engine, such as Razor, to display the data in a formatted fashion.

Here is an example of how to display data from a database using MVC and Entity Framework:

C#

```plaintext
public class StudentController : Controller
{
    private readonly StudentDbContext _context;

    public StudentController(StudentDbContext context)
    {
        _context = context;
    }

    public ActionResult Index()
    {
        var students = _context.Students.ToList();
        return View(students);
    }
}
```

In this example, we are creating a controller named `StudentController` which inherits from `Controller`. The controller has a constructor that takes a `StudentDbContext` object as a parameter. This object is used to connect to the database and manage the entities.

The `Index()` action method queries the database for all the students and returns the results to the view. The view then display the list of the students to the user.

Views can use Razor views, a templating engine that can dynamically display the HTML code. Here's an example of a Razor view that can be used to render the data from a database:

HTML

```plaintext
@{
    ViewData["Title"] = "Students";
}

<h1>Students</h1>

<table>
    <thead>
        <tr>
            <th>Name</th>
            <th>Age</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var student in Model)
        {
            <tr>
                <td>@student.Name</td>
                <td>@student.Age</td>
            </tr>
        }
    </tbody>
</table>
```

This view displays a table that containing the names and ages of all the students in the database.


---

Original Source: https://www.mindstick.com/forum/159852/view-data-from-database-using-mvc-entity-framework

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
