jqxGrid with SQL Binding and LargeJsonResult

In this post, we will use in the data binding a custom JsonResult Class for ASP.Net MVC to avoid MaxJsonLength Exceeded Exception. The class is called LargeJsonResult. Here’s its implementation:
using System;
using System.Web.Script.Serialization;
namespace System.Web.Mvc
{
public class LargeJsonResult : JsonResult
{
const string JsonRequest_GetNotAllowed = "This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet.";
public LargeJsonResult()
{
MaxJsonLength = 1024000;
RecursionLimit = 100;
}
public int MaxJsonLength { get; set; }
public int RecursionLimit { get; set; }
public override void ExecuteResult( ControllerContext context )
{
if( context == null )
{
throw new ArgumentNullException( "context" );
}
if( JsonRequestBehavior == JsonRequestBehavior.DenyGet &&
String.Equals( context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase ) )
{
throw new InvalidOperationException( JsonRequest_GetNotAllowed );
}
HttpResponseBase response = context.HttpContext.Response;
if( !String.IsNullOrEmpty( ContentType ) )
{
response.ContentType = ContentType;
}
else
{
response.ContentType = "application/json";
}
if( ContentEncoding != null )
{
response.ContentEncoding = ContentEncoding;
}
if( Data != null )
{
JavaScriptSerializer serializer = new JavaScriptSerializer() { MaxJsonLength = MaxJsonLength, RecursionLimit = RecursionLimit };
response.Write( serializer.Serialize( Data ) );
}
}
}
}
In a Controller class called CustomersController, we will implement a method called GetCustomers which returns LargeJsonResult. If you are new to using the jqxGrid with ASP .NET MVC, you can take a look at this help topic: jquery-grid-to-sql-database.htm
public LargeJsonResult GetCustomers()
{
var dbResult = db.Customers.ToList();
var customers = from customer in dbResult
select new
{
customer.CompanyName,
customer.ContactName,
customer.ContactTitle,
customer.Address,
customer.City
};
return new LargeJsonResult { Data = customers, JsonRequestBehavior = System.Web.Mvc.JsonRequestBehavior.AllowGet };
}
The last step is to create and populate the jqxGrid with data.
var source = {
datatype: "json",
datafields: [{ name: 'CompanyName' }, { name: 'ContactName' },
{ name: 'ContactTitle' }, { name: 'Address' }, { name: 'City'} ],
url: 'Customers/GetCustomers'
};
var dataAdapter = new $.jqx.dataAdapter(source);
$("#jqxgrid").jqxGrid({
source: dataAdapter,
theme: 'classic',
columns: [{ text: 'Company Name', datafield: 'CompanyName', width: 250 },
{ text: 'Contact Name', datafield: 'ContactName', width: 150 },
{ text: 'Contact Title', datafield: 'ContactTitle', width: 180 },
{ text: 'Address', datafield: 'Address', width: 200 },
{ text: 'City', datafield: 'City', width: 120}]
});
largejsonresult-jquery-grid

About admin


This entry was posted in ASP .NET, jQuery Plugins, jQuery UI, jQuery UI Plugins, jQuery UI Widgets, jQuery Widgets, jqxGrid and tagged , , , , , , , , , , , , , , , . Bookmark the permalink.



Leave a Reply