问题
I am using the Infragistics jQuery grid in my ASP .NET MVC application. My datasource is an ADO .NET Entity model referencing an SQL database. Here is the code from my controller to set up the grid and provide the datasource for it to pull from:
public ActionResult Index()
{
var model = new GridModel();
model.DataSourceUrl = Url.Action("GetInstrumentListData");
this.InitializeGridOptions(model);
return View(model);
}
public JsonResult GetInstrumentListData()
{
var model = new GridModel();
this.InitializeGridOptions(model);
model.DataSource = _db.InstrumentLists.OrderBy(x => x.Tag).AsQueryable<InstrumentList>();
return model.GetData();
}
private void InitializeGridOptions(GridModel model)
{
Code to create columns...
model.DefaultColumnWidth = "100px";
model.Width = "100%";
model.Height = "700px";
model.Features.Add(new GridFiltering());
var sort = new GridSorting();
sort.Mode = SortingMode.Multiple;
model.Features.Add(sort);
var paging = new GridPaging();
paging.PageSize = 30;
model.Features.Add(paging);
var selection = new GridSelection();
selection.Mode = SelectionMode.Row;
selection.MultipleSelection = true;
model.Features.Add(selection);
}
The grid was taking ages to display (25-40 secs) so I did some investigating and it's the model.GetData()
call in GetInstrumentListData()
that is taking up all the time. According to Intellisense, this function first performs data binding and generates the JsonResult object.
I thought that maybe since I was attempting to display a total of 1000 records (even though pagination is enabled and only displaying 30 each view) that maybe it was taking a while to convert those records into JSON, so I reduced the amount of records to 10 (from 1.2mb of JSON data to 12.5kb). There was no difference in time.
Here is the request tracing from Firebug.
Any ideas on what is happening?
EDIT: @allentranks' answer and @AlastairPitts comment made me realise that it is in fact the source I am getting my data from. The source isn't a table but a view, which was created by my DBA from a whole bunch of crazy joins. Turns out that it takes 13+ secs to run the query, so its no wonder its taking so long to load. Thanks for your help.
回答1:
I am not sure why your InstrumentLists need to be ordered twice in your query.
_db.InstrumentLists.OrderBy(x => x.Tag).AsQueryable<InstrumentList>().OrderBy(x => x.Tag);
this should work:
_db.InstrumentLists.OrderBy(x => x.Tag).ToList();
And, you maybe need to create an index on the Tag column to execute the query more quickly.
回答2:
There is something wrong with data that you are returning from controller (amount of data is too much. 12.5kb for 10 records!). you should inspect json data returned from the server in firebug. Make sure that you are only receiving data that is required and specific to the grid. i would suggest making a custom view model for the grid.
As a side note i would like to share that for a grid with 50 records per page and 11 columns the json data i receive is 1.9 kb and its 967 bytes.
来源:https://stackoverflow.com/questions/7169643/jquery-grid-binding-in-asp-net-mvc-is-so-slow