Please follow the steps given below to create an application that allows user to add, edit, delete and find records of student.
Open Microsoft Visual Studio 2010 - Select New Project - Select Web from the Installed Templates - Select ASP.NET MVC3 or MVC4 Web Application - Enter the project name Modal_CRUD_MVC in the Name textbox - Click OK.
After select the project you can see the following dialog box:
Select Empty from the Select a template option - Select Razor view engine - Click OK.
Add the following Modal classes (Student and StudentContext) in your Model folder:
Student class represents the table structure in the database and StudentContext Students property represents the name of table and data of the students in the database.
Also include Entity framework reference in your project because in this application we are going to use entity framework code first model to add, edit, delete and find records from the database. To include entity framework dll, follow these steps:
Go to Project - Manage NuGet Packages - Enter entity framework in the search textbox and search online.
You will find latest version of Entity framework version.
You also have to configure your web.config file for the implementation of this project. Add the connectionStrings element under configuration tag:
Add a Controller in your project and edit the controller name as HomeController as shown in the figure below:
After adding HomeController, add an Index view into your project:
Edit the Index view and add the following code as given below. Also include these files (jquery-ui.min.css, jquery-1.7.1.min.js, jquery-ui-1.8.20.min.js) in your project.
I have put all the code necessary to perform add, edit, delete and find records of the students in the Index view. In this project we do not have any Layout or Master page.
After including files in the project, your solution explorer might something look like this:
Add a partial view _StudentData in Views - Home folder.
Edit the _StudentData partial view as below:
Add a partial view _StudentDetails in Views - Home folder.
Edit the _StudentDetails partial view as below:
Add and Edit StudentDetails in Views - Home folder.
The purpose of adding StudentDetails view is same as I have explained above for StudentData view.
I have finished Model and View portion. Now I am going to explain about the HomeController and its Action methods which are necessary in order to run our application.
Index action returns a View along with the list of students:
Add the AddEditRecord action with the parameter (id) which can be either null or not null. If the id parameter is null it will return empty _StudentData or StudentData, partial view or View respectively otherwise it will find the record of student on the basis of id and passes the student model to the view in order to update the student details.
Here I am also creating a dynamic property ViewBag.IsUpdate to change the button type in view. If the IsUpdate is true it will set button text as Update and if IsUpdate is false then it will be set as Save.
Add the AddEditRecord action which is of HttpPost type to add or update student record. If the cmd contains Save it will add the record of student in the database otherwise update the data of student in the database.
To delete the record of students add the DeleteRecord action in the HomeController. This will delete the record of a student from the database on the basis of id.
To view the details of student, add the Details action in your HomeController. This will display the details of a student on the basis of id.
Now both coding and design part is over. To test an application, run or debug your application:
I have added few records during coding. You can add student records by using Create New option.
To edit records, just press edit link given for each students in a row.
To delete the record of student, use Delete link.
Thanks for reading this article. You can enter your valuable comments and suggestion to improve this article in the comment box.