MVC 4 Web Forms Database Tutorial357


In this tutorial, we will learn how to perform database operations in an MVC 4 Web Forms application. We will be using Entity Framework Code First approach to access the database.

Creating the Database

First, we need to create the database. To do this, we will use the Package Manager Console. Open the Package Manager Console by going to Tools > NuGet Package Manager > Package Manager Console.

In the Package Manager Console, type the following command and press Enter:```
Enable-Migrations -ContextTypeName MyDatabaseContext
```

This command will create a new folder named Migrations in your project. The Migrations folder will contain the database migration history.

Now, type the following command and press Enter:```
Add-Migration InitialCreate
```

This command will create a new migration named InitialCreate. The InitialCreate migration will create the database and the tables that we will use in our application.

Finally, type the following command and press Enter:```
Update-Database
```

This command will update the database to the latest migration.

Creating the Model

Next, we need to create the model classes that will represent our database tables. To do this, right-click on the Models folder in your project and select Add > Class.

In the Add New Item dialog box, enter the following class name and click Add:```
Student
```

In the Student class, add the following properties:```
public int StudentId { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
public string PhoneNumber { get; set; }
public string EmailAddress { get; set; }
```

These properties will correspond to the columns in the Students table in our database.

Creating the Controller

Now, we need to create the controller that will handle the database operations. To do this, right-click on the Controllers folder in your project and select Add > Controller.

In the Add Controller dialog box, enter the following controller name and click Add:```
StudentsController
```

In the StudentsController class, add the following using statements:```
using System;
using ;
using ;
using ;
using ;
using ;
```

Now, add the following action methods to the StudentsController class:```
public ActionResult Index()
{
using (MyDatabaseContext db = new MyDatabaseContext())
{
var students = ();
return View(students);
}
}
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(Student student)
{
using (MyDatabaseContext db = new MyDatabaseContext())
{
(student);
();
return RedirectToAction("Index");
}
}
public ActionResult Edit(int id)
{
using (MyDatabaseContext db = new MyDatabaseContext())
{
var student = (id);
return View(student);
}
}
[HttpPost]
public ActionResult Edit(Student student)
{
using (MyDatabaseContext db = new MyDatabaseContext())
{
(student).State = ;
();
return RedirectToAction("Index");
}
}
public ActionResult Delete(int id)
{
using (MyDatabaseContext db = new MyDatabaseContext())
{
var student = (id);
(student);
();
return RedirectToAction("Index");
}
}
```

These action methods will handle the CRUD operations for the Students table.

Creating the Views

Finally, we need to create the views that will display the data from the database. To do this, right-click on the Views folder in your project and select Add > View.

In the Add View dialog box, select the following options:
View name: Index
Model class: <>
Template: List

Click Add.

In the Index view, add the following code:```
@model IEnumerable
@{
= "Students";
}

Students

Name
Address
City
State
ZipCode
PhoneNumber
EmailAddress



@foreach (var student in Model)
{

@
@
@
@
@
@
@



}

```

This view will display a list of all the students in the database.

Now, add another view named Create.

In the Create view, add the following code:```
@model
@{
= "Create";
}

Create Student


Name:


Address:


City:


State:


ZipCode:


PhoneNumber:


EmailAddress:



```

This view will allow the user to create a new student.

Finally, add another view named Edit.

In the Edit view, add the following code:```
@model
@{
= "Edit";
}

Edit Student
@(model => )


Name:


Address:

2025-02-14


Previous:How to Enhance Your Vocabulary Through Systematic Learning

Next:Speedy Video Editing Made Easy: A Comprehensive Tutorial