In the previous chapter, we have learnt about how we use entity framework to create database layer and how we can easily use this in ASP.NET MVC Application. In this chapter, we are going to learn how we can use scaffolding technique to create a basic CRUDE application without writing any code.
What is Scaffolding:
In programming, Scaffolding is a technique through we can create some classes and some code from the model class or the database structure.
Scaffolding can be beneficial in certain scenarios like following.
- If you are learning MVC and you don’t know much about ASP.NET MVC, then at that time it will definitely help you to understand the structure of the ASP.NET.
- You want to explore some new things or add-on for visual studio or any other technique, then at that time you can quickly create a CRUD operation without wasting your time.
Following are some of the features that are supported in scaffolding:
- Supports VB.NET and C# both language
- It also supports stand view engines so we don’t have to create HTML code also.
- It will also take care about the existing master page template and CSS styles
How Scaffolding works:
Now, let’s see how it works. I am going to use same application and same database structure that we have used in previous chapter.
Therefore, as we know we have a table called users with userid as primary key. Following is a structure of User table:
In addition, I have also inserted some data into table for viewing purpose. So once we have our scaffolidng complete, at that time we can see the data in our application. Therefore, I have inserted data as follows:
Now we also have our Entity Framework Model class ready as following:
So now, we have all the prerequisites that we need to create a basic CRUD application with scaffolding. Please make sure you compile your application before starting the Scaffolding with ASP.NET MVC, as we need to have our model classes ready based on that we can use scaffolding. So now, once you are done with all this, right click controller folder and select Add and then add Controllers as follows:
Once you click on Controller, It will open up a add controller dialog like following:
Here you can see that there are several options in the dialog box. The first option is template which template you want to use. There are three options for that:
- Empty Controller
- Controller with Read/Write actions and Views, using entity framework
- Controllers with Empty/Read write operations.
Another option for choosing model classes where I have chosen our user model class.
Last option is views where I can select user view.
Now click Add, it will create all view controller like following:
Therefore, you can see that without writing any code we have created our application. So, it’s now time to run our application and see whether it is actually working or not. So once you run application, you will be first presented to the User View where you have all the data, which we have inserted into the application like following:
As you can see, data, which we have in database directly without writing code. In addition, there is a Create New link there on the top as well as also there are edit, details and delete links. Once you click Create New, following screens will presented to you where you have all the validation and other things ready like following:
Once you insert data and click on Create, it will redirect back to you all users view. Same way, you have all other functionalities ready. Once you click Edit from user’s grid, it will be redirected back to you with details pre-populated.
Same way once you click on Details link from users grid you will be redirected to the details page like following:
So, as you can see, this is very easy and we can create a Basic CRUD application without writing any code from the model classes.
In next chapter, we will understand code for writing CRUD application in ASP.NET.