Translate

Wednesday 30 January 2013

DataGrid Display Employee Details and Ascending Order In DataGrid :


DataGrid Display Employee Details and Ascending Order In DataGrid :

Model:

In Employee model Class i'm getting the values as a list and display in a razor view engine..

Employee.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVC3DataGridDisplayEmployee.Models
{
    public class Employee
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public double Salary { get; set; }
        public static List<Employee> GetList()
        {
            List<Employee> Employees = new List<Employee>
            {
                new Employee { FirstName = "jagan",LastName = "mohan",Salary = 45000},
                new Employee { FirstName = "Murali" ,LastName = "Dhar" , Salary = 75000 },
                new Employee { FirstName = "Uma" ,LastName = "Appu" , Salary = 60000 }
            };
            return Employees;
        }
    }
}

Controller:

EmployeesController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC3DataGridDisplayEmployee.Models;

namespace MVC3DataGridDisplayEmployee.Controllers
{
    public class EmployeesController : Controller
    {
        //
        // GET: /Employees/

       
        public ActionResult Show()
        {
            var Emp = Employee.GetList();
            return View(Emp);
        }

    }
}

View : Displaying the Employee details in a Web grid and sorted in ascending order.

Show.cshtml:

@model IEnumerable<MVC3DataGridDisplayEmployee.Models.Employee>
@{
    ViewBag.Title = "Employee List";
    ////WebGrid grid = new WebGrid(Model);
          
   var grid = new WebGrid(source: Model,
              
                rowsPerPage: 3);
}
<h2>People</h2>
<div id = "grid">
@grid.GetHtml(columns: new []
{
    grid.Column("FirstName"),
    grid.Column("LastName"),
    grid.Column("Salary",format:@<text>$@item.Salary</text>)
})
</div>

No comments:

Post a Comment