Click here to Skip to main content
15,896,453 members
Articles / Web Development / ASP.NET

AJAX based CRUD tables using ASP.NET MVC 3 and jTable jQuery plug-in

Rate me:
Please Sign up or sign in to vote.
4.95/5 (256 votes)
10 Feb 2013MIT19 min read 5M   55.5K   575  
Creating AJAX based CRUD tables using ASP.NET MVC 3 and the jTable jQuery plug-in.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Hik.JTable.Models;
using Hik.JTable.Repositories;
using Hik.JTable.Sessions;

namespace jTableWithAspNetWebForms
{
    public static class DemoMethods
    {
        private static IRepositoryContainer _repository { get { return RepositorySesssion.GetRepository(); } }

        #region Student methods

        public static object StudentList(int jtStartIndex, int jtPageSize, string jtSorting)
        {
            try
            {
                //Get data from database
                int studentCount = _repository.StudentRepository.GetStudentCount();
                List<Student> students = _repository.StudentRepository.GetStudents(jtStartIndex, jtPageSize, jtSorting);

                //Return result to jTable
                return new { Result = "OK", Records = students, TotalRecordCount = studentCount };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", Message = ex.Message };
            }
        }

        public static object StudentListByFilter(string name, int cityId, int jtStartIndex, int jtPageSize, string jtSorting)
        {
            try
            {
                //Get data from database
                int studentCount = _repository.StudentRepository.GetStudentCountByFilter(name, cityId);
                List<Student> students = _repository.StudentRepository.GetStudentsByFilter(name, cityId, jtStartIndex, jtPageSize, jtSorting);

                //Return result to jTable
                return new { Result = "OK", Records = students, TotalRecordCount = studentCount };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", Message = ex.Message };
            }
        }

        public static object CreateStudent(Student record)
        {
            try
            {
                var addedStudent = _repository.StudentRepository.AddStudent(record);
                return new { Result = "OK", Record = addedStudent };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", Message = ex.Message };
            }
        }

        public static object UpdateStudent(Student record)
        {
            try
            {
                _repository.StudentRepository.UpdateStudent(record);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", Message = ex.Message };
            }
        }

        public static object DeleteStudent(int StudentId)
        {
            try
            {
                _repository.StudentRepository.DeleteStudent(StudentId);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", Message = ex.Message };
            }
        }

        #endregion

        #region City methods

        public static object GetCityOptions()
        {
            try
            {
                var cities = _repository.CityRepository.GetAllCities().Select(c => new { DisplayText = c.CityName, Value = c.CityId });
                return new { Result = "OK", Options = cities };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", Message = ex.Message };
            }
        }

        #endregion

        #region Phone methods

        public static object PhoneList(int studentId)
        {
            try
            {
                var phones = _repository.PhoneRepository.GetPhonesOfStudent(studentId);
                return new { Result = "OK", Records = phones };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", Message = ex.Message };
            }
        }

        public static object DeletePhone(int phoneId)
        {
            try
            {
                _repository.PhoneRepository.DeletePhone(phoneId);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", Message = ex.Message };
            }
        }

        public static object UpdatePhone(Phone phone)
        {
            try
            {
                _repository.PhoneRepository.UpdatePhone(phone);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", Message = ex.Message };
            }
        }

        public static object CreatePhone(Phone phone)
        {
            try
            {
                var addedPhone = _repository.PhoneRepository.AddPhone(phone);
                return new { Result = "OK", Record = addedPhone };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", Message = ex.Message };
            }
        }

        #endregion

        #region Exam methods

        public static object ExamList(int studentId)
        {
            try
            {
                Thread.Sleep(200);
                var exams = _repository.ExamRepository.GetExamsOfStudent(studentId).OrderBy(e => e.ExamDate).ToList();
                return new { Result = "OK", Records = exams };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", Message = ex.Message };
            }
        }

        public static object DeleteExam(int studentExamId)
        {
            try
            {
                Thread.Sleep(50);
                _repository.ExamRepository.DeleteExam(studentExamId);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", Message = ex.Message };
            }
        }

        public static object UpdateExam(StudentExam exam)
        {
            try
            {
                _repository.ExamRepository.UpdateExam(exam);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", Message = ex.Message };
            }
        }

        public static object CreateExam(StudentExam exam)
        {
            try
            {
                var addedExam = _repository.ExamRepository.AddExam(exam);
                return new { Result = "OK", Record = addedExam };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", Message = ex.Message };
            }
        }

        #endregion

        #region Person methods

        public static object PersonList()
        {
            try
            {
                List<Person> peopleList = _repository.PersonRepository.GetAllPeople();
                return new { Result = "OK", Records = peopleList };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", Message = ex.Message };
            }
        }

        public static object CreatePerson(Person record)
        {
            try
            {
                var addedPerson = _repository.PersonRepository.AddPerson(record);
                return new { Result = "OK", Record = addedPerson };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", Message = ex.Message };
            }
        }

        public static object UpdatePerson(Person record)
        {
            try
            {
                _repository.PersonRepository.UpdatePerson(record);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", Message = ex.Message };
            }
        }

        public static object DeletePerson(int PersonId)
        {
            try
            {
                _repository.PersonRepository.DeletePerson(PersonId);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", Message = ex.Message };
            }
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Founder Volosoft
Turkey Turkey
I have started programming at 14 years old using Pascal as hobby. Then I interested in web development (HTML, JavaScript, ASP...) before university.

I graduated from Sakarya University Computer Engineering. At university, I learned C++, Visual Basic.NET, C#, ASP.NET and Java. I partly implemented ARP, IP and TCP protocols in Java as my final term project.

Now, I am working on Windows and web based software development mostly using Microsoft technologies in my own company.

My open source projects:

* ABP Framework: https://abp.io
* jTable: http://jtable.org
* Others: https://github.com/hikalkan

My personal web site:

https://halilibrahimkalkan.com

Comments and Discussions