In this article I will explain with an example, how to retrieve (display) data from Database in TextBoxes in ASP.Net MVC Razor.
The data (records) from Database will be fetched using Entity Framework and will be displayed in TextBoxes created using Model class in ASP.Net MVC Razor.
Note: For beginners in ASP.Net MVC and Entity Framework, please refer my article ASP.Net MVC: Simple Entity Framework Tutorial with example. It covers all the information needed for connecting and configuring Entity Framework.
 
 
Database
I have made use of the following table Customers with the schema as follows. CustomerId is an Auto-Increment (Identity) column.
Retrieve (Display) data from Database in TextBoxes in ASP.Net MVC
 
I have already inserted few records in the table.
Retrieve (Display) data from Database in TextBoxes in ASP.Net MVC
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 
Entity Framework Model
Once the Entity Framework is configured and connected to the database table, the Model will look as shown below.
Retrieve (Display) data from Database in TextBoxes in ASP.Net MVC
 
 
Controller
The Controller consists of the following Action method.
Action method for handling GET operation
Inside this Action method, a single record from the Customers table is returned to the View.
public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        CustomersEntities entities = new CustomersEntities();
        List<Customer> customers = entities.Customers.ToList();
 
        return View(customers.Find(p=> p.CustomerId == 2));
    }
}
 
 
View
Inside the View, in the very first line the Entity Framework Customer Model class is declared as Model for the View.
There are three TextBox fields created for displaying values for CustomerId, Name and Country using the Html.TextBoxFor method.
@model TextBox_From_DB_MVC.Customer
 
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    <table border="0" cellpadding="5" cellspacing="0">
        <tr>
            <td>Customer Id</td>
            <td>@Html.TextBoxFor(m => m.CustomerId)</td>
        </tr>
        <tr>
            <td>Name</td>
            <td>@Html.TextBoxFor(m => m.Name)</td>
        </tr>
        <tr>
            <td>Country</td>
            <td>@Html.TextBoxFor(m => m.Country)</td>
        </tr>
    </table>
</body>
</html>
 
 
Screenshot
Retrieve (Display) data from Database in TextBoxes in ASP.Net MVC
 
 
Downloads