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
- app.UseMvc(routes =>
- {
- routes.MapRoute(
- name: "default",
- template: "{controller=Home}/{action=Index}/{id?}");
- });
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.
Now press F5 to launch the application and navigate to Employee controller by appending /Employee in 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. Please refer to the attached code for better understanding.