This is a migrated thread and some comments may be shown as answers.

[Solved] Custom Server Binding and EF 4.1 Code First

0 Answers 42 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Hsd
Top achievements
Rank 1
Hsd asked on 23 May 2011, 10:21 PM
Has anyone got the MVC Grid custom server binding to work with models created using EF 4.1 Code First? I've tried modifying the MVC Grid example from the demo site (http://demos.telerik.com/aspnet-mvc/razor/grid/customserverbinding), but I get a NullReferenceException. The View is the same as the demo. The controller is below:

[GridAction(GridName = "Grid")]
public ActionResult CustomServerBinding(GridCommand command)
{
    IEnumerable data = GetServerData(!IsEmptyCommand(command) ? command : new GridCommand());
    return View(data);
}
private IEnumerable GetServerData(GridCommand command)
{
    DataLoadOptions loadOptions = new DataLoadOptions();
    loadOptions.LoadWith<Order>(o => o.Customer);
    var dataContext = new NorthwindDataContext();
    // EF 4.1 CODE FIRST GENERATED MODELS DO NOT HAVE A LOADOPTIONS
    //{
    //    LoadOptions = loadOptions
    //};
    IQueryable<Practice> data = dataContext.Order;
    //Apply filtering
    data = data.ApplyFiltering(command.FilterDescriptors);
    ViewData["total"] = data.Count();
    //Apply sorting
    data = data.ApplySorting(command.GroupDescriptors, command.SortDescriptors);
    //Apply paging
    data = data.ApplyPaging(command.Page, command.PageSize);
    //Apply grouping
    if (command.GroupDescriptors.Any())
    {
        return data.ApplyGrouping(command.GroupDescriptors);
    }
    return data.ToList();
}
private bool IsEmptyCommand(GridCommand command)
{
    return command.PageSize == 0;
}

Tags
Grid
Asked by
Hsd
Top achievements
Rank 1
Share this question
or