Disable Automatic DataBinding


無論Telerik或Kendo UI的Grid,在頁面render時必定會去讀取要綁在Grid上的資料,也就是執行MVC中的Action method並將資料呈現在UI上,
以Telerik為例(@Html.Telerik().Grid< mydocument>()
.Name("Grid")
.DataBinding(d => d.Ajax().Select("Query", "Home"))
)

以Kendo UI為例@(Html.Kendo().Grid< myDocument>()
.Name("Grid")
.DataSource(dataSource => dataSource.Ajax()
.ServerOperation(false)
.Read(read => read.Action("Query", "Home")))
)


但,如果你期望一開始不做任何DataBinding,只放個白白的Grid,等使用者確實下條件才去後端撈資料,那就要想辦法阻止automatic databinding這件事發生,
Telerik的做法,是利用OnDataBinding的event// 判斷是否第一次載入
var isFirstLoad = true;
function onDataBinding(e) {
// 當第一次載入,不呼叫 Grid Ajax Select,直接 return
if (isFirstLoad) {
e.preventDefault();
return;
}
}

定義Grid Event
.ClientEvents(c => c.OnDataBinding("onDataBinding"))

那Kendo UI呢?
根據官方說法,目前尚未提供可取消DataSource之RequestStart的event
Currently it is not possible to cancel the RequestStart event of the DataSource. 
We will provide such functionality in the future releases. I am sorry for the inconvenience.

既然前端沒有event可阻擋,那只好在後端撈資料時丟個空instance給Grid,一樣能binding不拋錯,希望未來能盡快release這個event,不然還要多跑一次Action method

沒有留言: