2015-08-23 123 views
2

我試圖用一個來自SQL的數據綁定一個Kendo網格。該表出現,但沒有填充任何數據。我無法弄清楚什麼是錯的。我感謝所有幫助!Kendo網格數據不能綁定

這是我的控制器:

public ActionResult Test() 
{ 
    return View(); 
} 

[HttpGet] 
public ActionResult Test_Read([DataSourceRequest] DataSourceRequest request) 
{ 
    return Json(GetCountries().ToDataSourceResult(request), JsonRequestBehavior.AllowGet); 
} 

[NonAction] 
private IQueryable<CountryViewModel> GetCountries() 
{ 
    return from user in database.Countries 
      select new CountryViewModel 
      { 
       Id = user.Id, 
       Name = user.Name, 
      }; 
} 

這是我的ViewModel:

public class CountryViewModel 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
} 

這是實體模型數據嚮導生成的模型汽車:

public partial class Country 
{ 
    public Country() 
    { 
     this.Cities = new HashSet<City>(); 
     this.Regions = new HashSet<Region>(); 
    } 

    public int Id { get; set; } 
    public string Name { get; set; } 
    public System.DateTime CreatedDate { get; set; } 

    public virtual ICollection<City> Cities { get; set; } 
    public virtual ICollection<Region> Regions { get; set; } 
} 

這是查看:

@(Html.Kendo().Grid<Traveler.Models.CountryViewModel>() 
     .Name("grid") 
     .DataSource(dataSource => dataSource 
      .Ajax() 
      .PageSize(20) 
      .Model(model => model.Id(p => p.Id)) 
      .Read(read => read.Action("Test_Read", "Home").Type(HttpVerbs.Get)) 
     ) 

     .Columns(columns => 
     { 
      columns.Bound(user => user.Id).Width(100); 
      columns.Bound(user => user.Name).Width(100); 
     }) 
     .Pageable() 
     .Sortable() 
     .ColumnMenu() 
     .Resizable(resize => resize.Columns(true)) 
     .Scrollable() 
     .HtmlAttributes(new { style = "height:100%;" }) 
     .Filterable() 
     .Reorderable(reorder => reorder.Columns(true)) 
     .ColumnMenu() 
     .Pageable(p => p.Enabled(true).Input(true).PageSizes(new[] { 10, 20, 50 }).Refresh(true)) 
) 
+1

幾個問題1)控制器首先被擊中? 2)如果它返回的是什麼數據(你是否檢查過提琴手或開發人員工具等)? –

+0

網格中有兩個'Pageable()'和'ColumnMenu()'。刪除多餘的。 – ataravati

+0

我有可能缺少參考嗎? – SAS20

回答

0

@(Html.Kendo().Grid<Traveler.Models.CountryViewModel>() 
 
     .Name("grid") 
 
    .EnableCustomBinding(true) 
 
     .DataSource(dataSource => dataSource 
 
      .Ajax() 
 
      .PageSize(20) 
 
      .Read(read => read.Action("Test_Read", "Home")) 
 
     ) 
 
     .Columns(columns => 
 
     { 
 
      columns.Bound(user => user.Id).Width(100); 
 
      columns.Bound(user => user.Name).Width(100); 
 
     }) 
 
     .Pageable() 
 
     .AutoBind(true) 
 
     .Sortable() 
 
     .Resizable(resize => resize.Columns(true)) 
 
     .Scrollable() 
 
     .Filterable() 
 
     .Reorderable(reorder => reorder.Columns(true)) 
 
)

我已經改變了一些代碼,檢查這將有助於。