0

我正在使用Kendo-ui JQuery版本,並且我試圖從ApiController中填充kendo-ui網格。我的網格保持空白......我錯過了什麼?從apicontroller中綁定kendo-ui網格

這裏是我的ApiController的結果是:〜/ API /國家

[{"Id":4,"Name":"Germany"}, 
{"Id":5,"Name":"China"}, 
{"Id":6,"Name":"Myanmar"}] 

這裏是我的ApiController代碼:

public class CountriesController : ApiController 
{ 
    private DBContext db = new DBContext(); 

    // GET api/Countries 
    [Queryable] 
    public IQueryable<Country> GetCountries() 
    { 
     return db.Countries; 
    } 
} 

這裏是我的CSHTML代碼:

<script type='text/javascript'> 

    $(document).ready(function() { 
     $("#grid").kendoGrid({ 
      columns: [ 
       { field: "Id", title: "id" }, 
       { field: "Name", title: "name" } 
      ], 
      dataSource: new kendo.data.DataSource({ 
       transport: { 
        read: "api/Countries" 
       }, 
       schema: { 
        model: { 
         id: "Id", 
         fields: { 
          Id: { type: "number" }, 
          Name: { type: "string" } 
         } 
        } 
       }, 
       pageSize: 3 
      }), 
      pageable: true 
     }); 
    }); 

</script> 

感謝您的幫助。

+0

如果您觀看網絡流量,是否有針對api/Countries的請求,如果有,請求代碼是什麼? –

+0

謝謝羅賓,我通常會檢查網絡流量,但這次我沒有這樣做。不知道爲什麼!無論如何,迴應是空的,因爲在我的情況下url應該是「/ api/Countries」而不是「api/Countries」。是作品! – DotNetBeliever

回答

0

在從劍道數據源我的API調用,我總是要指定它是一個json返回dataType,我認爲它默認爲jsonp。

dataSource: new kendo.data.DataSource({ 
    transport: { 
    read: { 
     url: "/api/Countries", 
     dataType: 'json' 
     } 
    }, 
    schema: { 
    model: { 
     id: "Id", 
     fields: { 
     Id: { type: "number" }, 
     Name: { type: "string" } 
     } 
    } 
    }, 
    pageSize: 3 
}), 
+0

如前所述(其他評論),問題是URL「api/Countries」而不是「/ api/Countries」。 – DotNetBeliever

0

Kendo Grid沒有以正確的格式恢復Json。 確保使用KendoMVC DataSourceRequest對象以正確的格式返回數據以供Grid使用。

下面是一個例子:

public ActionResult Update([DataSourceRequest] DataSourceRequest request, MyViewModel data) 
    { 
     var result = UpdateBackend(data); 
     return Json(result.ToDataSourceResult(request), JsonRequestBehavior.AllowGet); 
    } 

檢查出的MVC劍道演示頁面有更多的例子: http://demos.telerik.com/aspnet-mvc/grid/index

+0

謝謝喬納森。我有點失落。我正在使用JQuery Kendo UI,因此是客戶端站點。我以爲我不需要服務器端KendoMVC。我錯了嗎 ? 此外,我的問題特別是關於ApiController。我已經成功地使用了標準的MVC控制器,在json結果中添加了諸如{「data」:Kendo支持的內容。如果我是正確的,ApiController調用的結果可以是除json之外的其他格式。除了使用DataSourceRequest發送Kendo支持的結果之外,沒有其他方法嗎? – DotNetBeliever