Introduction
In this article, I am going to explain how to create an MVC web application in ASP.NET Core 2.0 using ADO.NET. We will be creating a sample Employee Record Management System and performing CRUD operations on it.
We will be using Visual Studio 2017 (Version 15.3.5 or above) and SQL Server.
Prerequisites
- Install .NET Core 2.0.0 or above SDK from here
- Install Visual Studio 2017 Community Edition (Version 15.3.5 or above) from here
Now, we are ready to proceed with the creation of our MVC web application.
Source Code
Before proceeding further i would recommend to download the source code from Github.
Creating Table and Stored Procedures
We will be using a DB table to store all the records of employees.
Open SQL Server and use the following script to create tblEmployee table.
Now, we will create stored procedures to add, delete, update, and get employee data.
To insert an Employee Record
To update an Employee Record
For deleting an Employee Record
Fetch all Employee Records
Now, our Database part has been completed. So, we will proceed to create the MVC application using Visual Studio.
Create MVC Web Application
Open Visual Studio and select File >> New >> Project.

After selecting the project, a “New Project” dialog will open. Select .NET Core inside Visual C# menu from the left panel.
Then, select “ASP.NET Core Web Application” from available project types. Put the name of the project as MVCDemoApp and press OK. Refer to this image.
Then, select “ASP.NET Core Web Application” from available project types. Put the name of the project as MVCDemoApp and press OK. Refer to this image.

After clicking on OK, a new dialog will open asking to select the project template. You can observe two drop-down menus at the top left of the template window. Select “.NET Core” and “ASP.NET Core 2.0” from these dropdowns. Then, select “Web application(Model-View-Controller)” template and press OK.

Now our project will open. You can observe that we have Models, Views and Controllers folders already created. We will be adding our files to these folders only.

Adding the Controller to the Application
Right click on Controllers folder and select Add >> New Item

An “Add New Item” dialog box will open. Select Web from the left panel, then select “MVC Controller Class” from templates panel, and put the name as EmployeeController.cs. Press OK.

Now our EmployeeController has been created. We will put all our business logic in this controller.
Adding the Model to the Application
Right click on Models folder and select Add >> Class. Name your class Employee.cs. This class will contain our Employee model properties.
Add one more class file to Models folder. Name it as EmployeeDataAccessLayer.cs . This class will contain our Database related operations.
Now, the Models folder has the following structure.

Open Employee.cs and put the following code in it. Since we are adding the required validators to the fields of Employee class, so we need to use System.ComponentModel.DataAnnotations at the top.
Open EmployeeDataAccessLayer.cs and put the following code to handle database operations. Make sure to put your connection string.
Now, we will proceed to create our Views.
Adding Views to the Application
To add views for our controller class, we need to create a folder inside Views folder with the same name as our controller and then add our views to that folder.
Right-click on the Views folder, and then Add >> New Folder and name the folder as Employee.

Now Right click on the Views/Employee folder, and then select Add >> New Item.

An “Add New Item” dialog box will open. Select Web from the left panel, then select “MVC View Page” from templates panel, and put the name as Index.cshtml. Press OK.

Thus we have created our first view. Similarly add 4 more views in Views/Employee folder, Create.cshtml, Delete.cshtml, Details.cshtml, and Edit.cshtml.
Now, our Views folder will look like this

Since our Views has been created, we will put codes in View and Controller for performing CRUD operations.
Create View
This view will be used to Add new employee data to the database.
Open Create.cshtml and put following code into it.
To handle database operations, we will create an object of EmployeeDataAccessLayer class inside the EmployeeController class.
To handle the business logic of create, open EmployeeController.cs and put following code into it.
The [Bind] attribute is used with parameter “employee” to protect against over-posting.To know more about over-posting visit here
Index View
This view will be displaying all the employee records present in the database. Additionally, we will also be providing action methods Edit, Details and Delete on each record.
Open Index.cshtml and put following code in it
To handle the business logic of Index view, open EmployeeController.cs and add following code in Index method.
Edit View
This view will enable us to edit an existing employee data.
Open Edit.cshtml and put following code into it.
To handle the business logic of Edit view, open EmployeeController.cs and add following code to it.
As you can observe that we have two Edit action methods, one for HttpGet and another for HttpPost.The HttpGet Edit action method will fetch the employee data and populates the fields of edit view. Once the user clicks on Save button after editing the record, a Post request will be generated which is handled by HttpPost Edit action method.
Details View
This view will display the details of a particular employee.
Open Details.cshtml and put following code into it.
To handle the business logic of Details view,open EmployeeController.cs and add following code to it.
Delete View
This view will help us to remove employee data .
Open Delete.cshtml and put following code into it.
To handle the business logic of Delete view, open EmployeeController.cs and add following code to it.
To complete Delete operation we need two Delete methods accepting same parameter (Employee Id). But two methods with same name and method signature will create a compile time error and if we rename the Delete method then routing won’t be able to find it as asp.net maps URL segments to action methods by name. So, to resolve this issue we put ActionName(“Delete”) attribute to the DeleteConfirmed method. That attribute performs mapping for the routing system so that a URL that includes /Delete/ for a POST request will find the DeleteConfirmed method.
When we click on Delete link on the Index page, it will send a Get request and return a View of the employee using HttpGet Delete method. When we click on Delete button on this view, it will send a Post request to delete the record which is handled by the HttpPost DeleteConfirmed method. Performing a delete operation in response to a Get request (or for that matter, performing an edit operation, create operation, or any other operation that changes data) opens up a security hole. Hence, we have two separate methods.
And that’s it. We have created our first ASP.NET Core MVC web application. Before launching the application, we will configure route URLs. Open Startup.cs file to set the format for routing.Scroll down to app.UseMvc method, where you can set the route url.Make sure that your route url is set like this
This url pattern sets HomeController as default controller and Index method as default action method, whereas Id parameter is optional. Default and optional route parameters need not be present in the URL path for a match. If we do not append any controller name in the URL then it will take HomeController as default controller and Index method of HomeController as default action method. Similarly, if we append only Controller name in the URL, it will navigate to Index action method of that controller.
Execution Demo
Now press F5 to launch the application and navigate to Employee controller by appending /Employeein the URL.
You can see the page as shown below.

Click on CreateNew to navigate to Create view. Add a new Employee record as shown in the image below.

If we miss the data in any field while creating employee record, we will get a required field validation error message.

After inserting the data in all the fields, click on “Create” button. The new employee record will be created and you will be redirected to the Index view, displaying records of all the employees. Here, we can also see action methods Edit, Details, and Delete.

If we want to edit an existing employee record, then click Edit action link. It will open Edit View as below where we can change the employee data.

Here we have changed the Department of employee Swati from Finance to HR.Click on “Save” to return to the Index view to see the updated changes as highlighted in the image below.

If we miss any fields while editing employee records, then Edit view will also throw required field validation error message

If you want to see the details of any Employee, then click on Details action link, which will open the Details view, as shown in the image below.

Click on “Back to List” to go back to Index view. Now, we will perform Delete operation on an employee named Venkat. Click on Delete action link which will open Delete view asking for a confirmation to delete.

Once we click on Delete button, it will send HttpPost request to delete employee record and we will be redirected to the Index view. Here, we can see that the employee with name Venkat has been removed from our record.

Conclusion
We have learned about creating a sample MVC web application with ASP.Net Core 2.0 using ADO.NET and SQL server. Post your valuable feedback in comment section.


0 comments:
Post a Comment
Note: only a member of this blog may post a comment.