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

ASP.NET MVC Flexigrid sample

Rate me:
Please Sign up or sign in to vote.
4.75/5 (40 votes)
31 Oct 2008CPOL7 min read 576.2K   18.2K   189  
How to create an ASP.NET MVC sample using LINQ to SQL, Flexigrid for JQuery, and JSON.
using System;
using System.Collections.Generic;
using System.Data.Linq.SqlClient;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Reflection;
using Flexigrid.Models;

namespace Flexigrid.Controllers
{
	public class JsonController : Controller
	{
		public ActionResult CustomersList ()
		{
			var db = new CustomerDataContext ();
			var q = from c in db.Customers
					select c;

			List<Customer> customers = q.ToList ();
			FlexigridObject flexigridObject = new FlexigridObject ();
			flexigridObject.page = 1;
			flexigridObject.total = db.Customers.Count ();
			foreach (Customer customer in customers)
			{
				FlexigridRow row = new FlexigridRow ()
				{
					id = customer.CustomerID,
					cell = GetPropertyList (customer)
				};
				flexigridObject.rows.Add (row);
			}

			return View ("FlexigridObject", flexigridObject);
		}

		public ActionResult FlexigridList ()
		{
			int page = int.Parse (Request.Form["page"]);
			int rp = int.Parse (Request.Form["rp"]);
			string qtype = Request.Form["qtype"].ToString ();
			string query = Request.Form["query"].ToString ();
			string sortname = Request.Form["sortname"].ToString ();
			string sortorder = Request.Form["sortorder"].ToString ();

			var db = new CustomerDataContext ();
			var q = from c in db.Customers
					select c;

			if (!string.IsNullOrEmpty (qtype) && !string.IsNullOrEmpty (query))
			{
				q = q.Like (qtype, query);
			}

			q = q.Skip ((page - 1) * rp).Take (rp);

			if (!string.IsNullOrEmpty (sortname) && !string.IsNullOrEmpty (sortorder))
			{
				q = q.OrderBy (sortname, (sortorder == "asc"));
			}

			List<Customer> customers = q.ToList ();
			FlexigridObject flexigridObject = new FlexigridObject ();
			flexigridObject.page = page;
			flexigridObject.total = db.Customers.Count ();
			foreach (Customer customer in customers)
			{
				FlexigridRow cell = new FlexigridRow ()
				{
					id = customer.CustomerID,
					cell = GetPropertyList (customer)
				};
				flexigridObject.rows.Add (cell);
			}

			return View ("FlexigridObject", flexigridObject);
		}

		private List<string> GetPropertyList (object obj)
		{
			List<string> propertyList = new List<string> ();

			Type type = obj.GetType ();
			PropertyInfo[] properties = type.GetProperties (BindingFlags.Instance | BindingFlags.Public);
			foreach (PropertyInfo property in properties)
			{
				object o = property.GetValue (obj, null);
				propertyList.Add (o == null ? "" : o.ToString ());
			}

			return propertyList;
		}
	}

	public static class ExtensionMethods
	{
		public static IQueryable<T> OrderBy<T> (this IQueryable<T> source, string propertyName, bool asc)
		{
			var type = typeof (T);
			string methodName = asc ? "OrderBy" : "OrderByDescending";
			var property = type.GetProperty (propertyName);
			var parameter = Expression.Parameter (type, "p");
			var propertyAccess = Expression.MakeMemberAccess (parameter, property);
			var orderByExp = Expression.Lambda (propertyAccess, parameter);
			MethodCallExpression resultExp = Expression.Call (typeof (Queryable), methodName, new Type[] { type, property.PropertyType }, source.Expression, Expression.Quote (orderByExp));
			return source.Provider.CreateQuery <T> (resultExp);
		}

		public static IQueryable<T> Like<T> (this IQueryable<T> source, string propertyName, string keyword)
		{
			var type = typeof (T);
			var property = type.GetProperty (propertyName);
			var parameter = Expression.Parameter (type, "p");
			var propertyAccess = Expression.MakeMemberAccess (parameter, property);
			var constant = Expression.Constant ("%" + keyword + "%");
			MethodCallExpression methodExp = Expression.Call (null, typeof(SqlMethods).GetMethod ("Like", new Type[] {typeof (string), typeof (string)}), propertyAccess, constant);
			Expression<Func<T, bool>> lambda = Expression.Lambda<Func<T, bool>> (methodExp, parameter);
			return source.Where (lambda);
		}
	}
}

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 Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions