To create a simple MVC application use the following procedure.
I have created a simple MVC application using Visual Studio 2010 and .Net Framework 4.0 and SQL Server 2008 using the following:
- Open Visual Studio 2010, select "File" -> "New" -> "Project...". The New Project Window will open. Select the ASP.NET MVC3 Web Application template, change the Name, change the Location if required, then select "OK". The New ASP.NET MVC3 Project Window will open. Select "Empty template" then press the "OK" button.
- Open SQL Server, create a database named "Test", create a table named "employee" with columns such as Code as Numeric, ID as string, Name as string and Department as string.
- After creating the database, go to the Solution Explorer of Visual Studio and right-click on the model folder. Then go to "Add" -> "New Item". The Add New Item Window will open, select ADO.NET Entity Data Model. Rename the entity data model to test, than press "Ok".
- Select "Generate from database" from the Entity Data Model Wizard Window and press "Next", select an existing connection to create a new connection. Then press the "Next" button.
- Select the table or tables, if required rename the model name, then press "Ok"; the selected table will appear in the test.edmx file.
- Again go to the Solution Explorer, right-click on the Controllers folder, click "Add Controller". In the Add Controller window rename the controller to "Empcontroler", select the option "Add action for Create, Update, Delete and Detail Scenario". Then press "Add".
- The EmpControler class will appear in the Visual Studio. Create the object of the entity class that you created when you created the model. The code is given below.
- Build the application, so the employee model class will appear while creating the view for your application.
-
- Models.testEntities TE = new Models.testEntities();
- Then right-click on the index function and select "Add View", rename that view if required. Select "Create a Strongly-typed View" than select "Employee Model", then select Scaffold template as to list. Then press "Add".
- After creating the view write the following code in the index function:
- public ActionResult Index()
- {
-
- List<Models.Employee> emp = TE.Employees.ToList();
-
- return View(emp);
- }
- Again right-click on the Detail function and click "Add view", select scaffold as to Details. In the Details function write the following code:
- public ActionResult Details(int id)
- {
- Models.Employee emp = TE.Employees.Where(x => x.Code == id).Single();
- return View(emp);
- }
- In the post create function, like in the preceding steps to create the view, select the scaffold to create and write the following code:
- [HttpPost]
- public ActionResult Create(Models.Employee emp)
- {
- try
- {
- TE.Connection.Open();
- TE.Employees.AddObject(emp);
- TE.SaveChanges();
- TE.AcceptAllChanges();
- return RedirectToAction("Index");
- }
- catch
- {
- return View();
- }
- }
- Create a view to edit and write the following code:
- public ActionResult Edit(int id)
- {
- TE.Connection.Open();
-
- Models.Employee emp = TE.Employees.Where(x => x.Code == id).Single();
- return View(emp);
- }
-
- [HttpPost]
- public ActionResult Edit(int id, Models.Employee emp)
- {
- Try
- {
-
- Models.Employee emp1 = TE.Employees.Where(x => x.Code == id).Single();
- emp1.Name=emp.Name;
- emp1.Department=emp.Department;
- TE.AcceptAllChanges();
- TE.SaveChanges();
- return RedirectToAction("Index");
- }
- Catch
- {
- return View();
- }
- }
- Create the view for the delete and write the following code:
- public ActionResult Delete(int id)
- {
- Models.Employee emp = TE.Employees.Where(x => x.Code == id).Single();
- return View(emp);
- }
-
- [HttpPost]
- public ActionResult Delete(int id, FormCollection collection)
- {
- try
- {
-
- Models.Employee emp = TE.Employees.Where(x => x.Code == id).Single();
- TE.Employees.DeleteObject(emp);
- TE.SaveChanges();
- TE.AcceptAllChanges();
- return RedirectToAction("Index");
- }
- Catch
- {
- return View();
- }
- }
Run the application and do a create, update and delete of your data. Please feel free to ask any questions.