ASP.Net MVC and dynamic flexigrid quick example

I’ve  just been playing around with Flexigrid for jquery.

Here’s a simple example on how to structure the json data in ASP.Net.

Flexigrid needs a json data format like this:

{
	"page":1,
	"total":30,
	"rows":
	[
		{"id":"1","cell":["16/12/2010 16:03:45","test","system"]},
		{"id":"2","cell":["16/12/2010 16:04:45","test","system"]}
	]
}

To generate that format in c# using LINQ, simply do something this:

var logs = service.LogList();

var rows = from l in vm.Logs
select new
{
	id = l.LogId.ToString(),
	cell = new List
	{
		l.LoggedDateTime.Value.ToString(),
		l.Message,
		l.System
	}
};

var data = new
{
	page = 1,
	total = logs.Count(),
	rows = rows
};

JsonResult jr = new JsonResult { Data = data, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s