jeffkenn
jeffkenn

Reputation: 201

MVC Telerik grid not binding only displaying json data in browser

I am trying to get a telerik grid to display json data that is being return from a controller action but the only it displays the actual json data in the browser window.

  1. Am i supposed to call .BindTo after read?
  2. Am i doing something wrong in my action?
  3. am i going about this all wrong?

        [HttpGet]
    public ActionResult ReadLeads([DataSourceRequest]DataSourceRequest request)
    {
    
    
        var model = new RecordLookupViewModel();
    
        using (var db = new RGI_MasterEntities())
        {
    
            db.Configuration.ProxyCreationEnabled = false;
    
            var results = db.tblMasterLeads
                .Where(
                    x => (model.FirstName == null || x.FirstName.Equals("Eric"))
                         && (model.RecordType == null || x.MasterLeadType.Equals("Responder"))
                )
                .Select(s => new LookupGridResults
                {
                    FirstName = s.FirstName,
                    LastName = s.LastName,
                    City = s.city,
                    State = s.state,
                    County = s.county,
                    Zip = s.zip
                }).Take(10);
    
            var result = results.ToDataSourceResult(request);
    
            return Json(result, JsonRequestBehavior.AllowGet);
    
        }
    } 
    

Hers is my view code for the grid.

                                @(Html.Kendo().Grid<LookupGridResults>()
.Name("grid")
.AutoBind(false)
.Columns(columns =>
{
    columns.Bound(p => p.FirstName).Filterable(ftb => ftb.Cell(cell => cell.Operator("contains"))).Width(225);
    columns.Bound(p => p.LastName).Width(225).Filterable(ftb => ftb.Cell(cell => cell.Operator("contains")));
    columns.Bound(p => p.City).Width(225).Filterable(ftb => ftb.Cell(cell => cell.Operator("contains")));
    columns.Bound(p => p.County).Width(225).Filterable(ftb => ftb.Cell(cell => cell.Operator("contains")));
    columns.Bound(p => p.State).Width(225).Filterable(ftb => ftb.Cell(cell => cell.Operator("contains")));
    columns.Bound(p => p.Zip).Width(225).Filterable(ftb => ftb.Cell(cell => cell.Operator("contains")));
})
.Pageable()
.Sortable()
.Scrollable()
.Filterable(ftb => ftb.Mode(GridFilterMode.Row))
.HtmlAttributes(new { style = "height:550px;" })
.DataSource(dataSource => dataSource
    .Ajax()
    .PageSize(20)
    .ServerOperation(true)
    .Read(read => read.Action("ReadLeads", "LeadsManagement").Type(HttpVerbs.Get))

 )

                                )

Here are my results btw.

{"Data":[{"LastName":"COFFEY","FirstName":"EDWARD","City":"FRANKFORT","County":"FRANKLIN","State":"KY","Zip":"40601-2304"},{"LastName":"DESPAIN","FirstName":"TONY","City":"CAMPBELLSVILLE","County":"TAYLOR","State":"KY","Zip":"42718-9397"},{"LastName":"HALBIG","FirstName":"RONALD","City":"CAMPBELLSVILLE","County":"TAYLOR","State":"KY","Zip":"42718-1556"},{"LastName":"KRAUS","FirstName":"REBECCA","City":"FRANKFORT","County":"FRANKLIN","State":"KY","Zip":"40601-2714"},{"LastName":"LAWLESS","FirstName":"MEREDITH","City":"CAMPBELLSVILLE","County":"TAYLOR","State":"KY","Zip":"42718-1556"},{"LastName":"RANKIN","FirstName":"PAULINE","City":"LAWRENCEBURG","County":"ANDERSON","State":"KY","Zip":"40342-1374"},{"LastName":"SHIRLEY","FirstName":"LORRAINE","City":"CAMPBELLSVLLE","County":"TAYLOR","State":"KY","Zip":"42718-1557"},{"LastName":"STAPLES","FirstName":"DAMON","City":"HODGENVILLE","County":"LARUE","State":"KY","Zip":"42748-1208"},{"LastName":"WILLIAMS","FirstName":"LUCY","City":"FRANKFORT","County":"FRANKLIN","State":"KY","Zip":"40601-2308"},{"LastName":"WILSON","FirstName":"BELIDA","City":"FRANKFORT","County":"FRANKLIN","State":"KY","Zip":"40601-1321"}],"Total":10,"AggregateResults":null,"Errors":null}

Upvotes: 1

Views: 575

Answers (1)

jeffkenn
jeffkenn

Reputation: 201

Thanks for all the help, it seemed i was missing a reference to a bundle. I do credit Mark Schultheiss for pointing me in the right direction.

Got it completly working today. Here is what fixed it.

  1. I changed my actionresult to a JsonResult.
  2. I had filtering turned on in the grid but none of my columns had filtering attributes.

I think thats about it. It works great now.

Upvotes: 1

Related Questions