Scaffolding Example in ASP.NET MVC 5 Step by Step

In this tutorial, you will learn:
1. What is Scaffolding in ASP.NET MVC 5?
2. What is the use of Scaffolding?
3. How to add Scaffolded Item in project?

What is Scaffolding in ASP.NET MVC 5?

Scaffolding is a code generation framework that automatically adds codes and creates view pages and controllers for your project. Scaffolding makes developer job easier by creating controllers and view pages for the data model. It generates codes and pages for CRUD(Create, Read, Update and Delete) Operation. In this chapter, I will show you how to use Scaffolding in your project. It is a complete programming example and contains step by step guide so if you are new in MVC then absolutely don’t worry and start reading this chapter.

How to Use Scaffolding in ASP.NET MVC 5?

Step 1. Open Visual Studio and Create New ASP.NET MVC 5 Project ScaffoldProject.
Step 2. Install Entity Framework in your Project. Right click on your Project Name Manage NuGet Packages. Browse Entity Framework and click on to Install button to it.
Step 3. Create a Model ItemListModel. Right click on Model Add Class. Give class name as ItemListModel.cs and click Add button.
Item List Model
Step 4. Add the following code into ItemListModel class.
using System.Data.Entity;

namespace ScaffoldProject.Models
{
    public class ItemListModel
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Category { get; set; }
        public decimal Price { get; set; }

        public class ItemListContext : DbContext
        {
            public DbSet<ItemListModel> itemListModel { get; set; }
        }
    }
}

 
Step 5. Build the application by pressing Ctrl + Shift + B.
Step 6. Add Scaffolded item for this model. Right click on Controller Add New Scaffolded Item.
Add Scaffolded Item
Step 7. Select MVC 5 Controller with views, using Entity Framework and click Add button.
MVC 5 Controller with views, using Entity Framework
Step 8. Select Details as follows:
a. Model Class: ItemListModel (ScaffoldProject.Models) b. Data Context class: Create a new data context class by clicking on + button. Add Data Context Class

c. Set Controller name: ItemListController

Add Item
Step 9. Now, you can see in solution explorer that ItemListController is created with its respective views.
Solution Explorer
Step 10. Run Your Project by Pressing F5 and see the magic of Scaffolded Framework.

Navigate to following link: http://localhost:3083/ItemList/

Output Images:
Output 1
Output 2
Output 3

Summary

In this chapter, you learned what Scaffolded item is in the ASP.NET MVC 5 and how to use it in the program. Scaffolded makes developer job easier and creates controllers and views automatically based on models and database tables. In the next chapter, you will learn Model Binding in ASP.NET MVC 5.

More Articles

 

Share your thought