using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using System.Linq.Expressions;
namespace MvcApplication30.Models
{
public class Class1
{
public int Student_ID { get; set; }
public string Student_Name { get; set; }
public string Student_Branch { get; set; }
public string Student_City { get; set; }
public string Student_State {get;set;}
}
public static class SortExtension
{
public static IOrderedEnumerable<TSource> OrderByWithDirection<TSource, TKey>
(this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
bool descending)
{
return descending ? source.OrderByDescending(keySelector)
: source.OrderBy(keySelector);
}
public static IOrderedQueryable<TSource> OrderByWithDirection<TSource, TKey>
(this IQueryable<TSource> source,
Expression<Func<TSource, TKey>> keySelector,
bool descending)
{
return descending ? source.OrderByDescending(keySelector)
: source.OrderBy(keySelector);
}
}
public class ModelServices : IDisposable
{
private readonly StudentEntities1 entities = new StudentEntities1();
public bool SaveStudent(string name, string branch, string city, string state)
{
try
{
IT_Student stdnt = new IT_Student();
stdnt.Student_Name = name;
stdnt.Student_Branch = branch;
stdnt.Student_City = city;
stdnt.Student_State = state;
entities.IT_Student.Add(stdnt);
entities.SaveChanges();
return true;
}
catch
{
return false;
}
}
public bool UpdateStudent(int id, string name, string branch, string city, string state)
{
try
{
var stdnt = (from tbl in entities.IT_Student
where tbl.Student_Id == id
select tbl).FirstOrDefault();
stdnt.Student_Name = name;
stdnt.Student_Branch = branch;
stdnt.Student_City = city;
stdnt.Student_State = state;
entities.SaveChanges();
return true;
}
catch
{
return false;
}
}
public bool DeleteStudent(int id)
{
try
{
var stdnt = (from tbl in entities.IT_Student
where tbl.Student_Id == id
select tbl).FirstOrDefault();
entities.IT_Student.Remove(stdnt);
entities.SaveChanges();
return true;
}
catch
{
return false;
}
}
//For Custom Paging
public IEnumerable<IT_Student> GetStudentPage(int pageNumber, int pageSize, string sort, bool Dir)
{
if (pageNumber < 1)
pageNumber = 1;
if (sort == "name")
return entities.IT_Student.OrderByWithDirection(x => x.Student_Name, Dir)
.Skip((pageNumber - 1) * pageSize)
.Take(pageSize)
.ToList();
else if (sort == "state")
return entities.IT_Student.OrderByWithDirection(x => x.Student_State, Dir)
.Skip((pageNumber - 1) * pageSize)
.Take(pageSize)
.ToList();
else if (sort == "city")
return entities.IT_Student.OrderByWithDirection(x => x.Student_City, Dir)
.Skip((pageNumber - 1) * pageSize)
.Take(pageSize)
.ToList();
else
return entities.IT_Student.OrderByWithDirection(x => x.Student_Id, Dir)
.Skip((pageNumber - 1) * pageSize)
.Take(pageSize)
.ToList();
}
public int CountStudent()
{
return entities.IT_Student.Count();
}
public void Dispose()
{
entities.Dispose();
}
}
public class PagedStudentModel
{
public int TotalRows { get; set; }
public IEnumerable<IT_Student> It_Student { get; set; }
public int PageSize { get; set; }
}
}