ASP.net MVC 4.0: CRUD & Sorting Opreation in SQL server compact edition through ADO.net Entity Data Model

1. Add New MVC Project

mvc1

 

2. Select template Internet Application. 

mvc2

 

3. Add database in App_Data folder

mvc3

 

4. Create table in your database

mvc4

 

5. Add New Item select ADO.NET Entity Data Model

mvc5

 

Complete Entity data model wizard

mvc6

 

6. First Run the application then Add Controller

In Scaffolding option

Select template->MVC controller with read/write actions and views, using Entity Framework

Select Model class-> This is the name of your database table name.

Select Data context class->This is the name of your Database name Entities

mvc7

7. Add new menu  Goto Views->Shared->Layout.cshtml

mvc12

 

8. Insert your Primary key field Goto Views->Default1->Index.cshtml & Details.cshtml

mvc13

9. Run The Application & Select Employee in menu

mvc14

 

10. Create New

mvc15

 

11. Enter data in field & click create button

mvc9

mvc8

You can view Details, Edit, Delete records……

12. Check your database

mvc10

For Sorting Record by click on the field name

1. Goto Controllers->Default1controller.cs  edit code in ActionResult Index() as

public ActionResult Index(String  sortorder)
        {
            ViewBag.NameSortParm = String.IsNullOrEmpty(sortorder)?"nam":"";
            ViewBag.CodeSortParm = String.IsNullOrEmpty(sortorder) ? "cod" : "";
            ViewBag.SalSortParm = String.IsNullOrEmpty(sortorder) ? "sal" : "";
            var emp = from s in db.employees select s;
            switch (sortorder)
            {
                case "nam":
                    {
                        emp = from s in db.employees select s;
                        emp = emp.OrderBy(s => s.ename);                      
                       
                        break;
                    }
                case "cod":
                    {
                        emp = from s in db.employees select s;
                        emp = emp.OrderBy(s => s.ecode);

                        break;
                    }
                case "sal":
                    {
                        emp = from s in db.employees select s;
                        emp = emp.OrderBy(s => s.esalary);

                        break;
                    }            
                
            }           
           
             return View(emp.ToList());
        }
    
    
    

2. Goto Views->Default1->Index.cshtml &  replace code from

ssort

 

After replacing it will have to write code as

ssort1

 

3. Run the Application Click Employee menu

sort_mvc1

 

4. Now sorting record by click on the Field name

sort_mvc1