Creating a ASP MVC CRUD application
A few years ago, creating a web application with create, read, update and delete (CRUD) functionality with traditional ASP or ASP.NET web forms could become cumbersome. In traditional ASP, developers were left with mixing presentational HTML markup with server-side Visual Basic script, which made creation and maintenance of applications difficult.
When ASP.NET web forms debuted, it shifted the paradigm where developers could at least separate presentational HTML markup from server-side business logic with their preferred object-oriented language (Visual Basic or C#.NET). However, web forms came with out of the box web controls that cornered developers into data controls which often felt bloated and made HTML output rendered by the application server (Internet Information Services) hard to debug.
With the radical shift in development with ASP MVC developers can create web applications like CRUD relatively easy and separate behaviors of concern. Developers have complete control over HTML markup, and the ability to separate classes based on their intended behaviors.
In the following tutorial, we’ll see how we can create a simple applicant system with ASP MVC. We’ll explore how we can either use the existing HTML markup scaffolding Microsoft provides, or create our own. We’ll see a glimpse of the new database access framework known as entity framework and how it can alleviate some of the common mistakes developers using in-line structured query language (SQL) make. We’ll understand how Razor web page views communicate via implicit GET and explicit POST commands. Lastly, we’ll start to see how ASP MVC delivers separation of concerns throughout an application.
If you would like to create an application such as this one, please do follow along in this step-by-step tutorial.
|Download Project Files|
Before beginning we need to make sure we have the following software installed and operational:
- Microsoft .NET framework or higher
- ASP MVC 4 or higher
- Microsoft Visual Studio
- Microsoft SQL Server Management Studio
- Make sure to install the local database service and management studio product
Once you have downloaded and installed these applications, open Visual Studio by following these steps:
- From the desktop, left click Start button
- In the search field, type visual studio
- Left click to select Visual Studio
Creating the project
From Visual Studio, follow these steps:
- From the main menu, choose File : New : Project
In the New Project window:
- From the left pane, leave Visual C# as selected
- From the top combo box, choose .NET Framework 4.5.1
- From the main window, choose ASP.NET Web Application
Next, while in the New Project window, at the bottom:
- In the name field, type Applicant
- In the location field, choose root of C:\
- In the solution field, should be auto filled with the same name as the name field (1.)
Lastly, while in the New Project window, at the bottom right:
- In the check box for Create directory for solution, leave checked
- In the check box for Add to Source Control, leave unchecked
- Once finished, click OK
Note: Regarding source control, once we have this project finished, pushing the files into a source control repository such as Team Foundation Server (TFS), Subversion, GitHub or CodePlex should be a straightforward task.
In the next window, choose (1) MVC and click (2) OK:
Once Visual Studio has completed creating the project, we should see from the right side in solution explorer we have a functional web application created with the appropriate folder structure needed for MVC. Go ahead and press F5 on the keyboard to ensure the project builds and launches a web page as shown below:
Entity framework with MVC applications
In the latest versions of the .NET framework, Microsoft has provided a new and more robust approach to data access with ASP MVC applications called entity framework. Entity framework aims to solve some of the common problems associated to data access retrieval and submission from ASP MVC application including but not limited to:
- Performance of complex queries: if your application is using complex stored procedures with complex business logic, it may be a worthwhile exercise to determine if using entity framework can speed up the query operation and make the overall query structure easier to write and understand.
- Native support of database operations: much like Active Data Objects (ADO), entity framework has native methods to perform common database operations such as selecting, filtering, sorting, adding, updating and deleting data from a database
- Native support of parameterized queries: with traditional ASP applications and ADO, we have to explicitly parameterize queries that deal with insertion of data into a database from an application to prevent the use of structured query language (SQL) injection attacks. With entity framework, parameterized queries are natively supported
For the purposes of this tutorial, we’ll be using entity framework and a deeper dive using entity framework will be covered later.
We'll continue next with creating our database.