首页 > 解决方案 > 带有自定义数据源的 kendo mvc 网格,该数据源具有空 id 行

问题描述

我想用我在更改数据源方面没有发言权的声明来打开这个问题,数据源是数据库中的一个函数,也在其他地方使用。

我有一个剑道网格,我正在从这样的视图模型加载:

 var data = db.Database.SqlQuery<OdometerLogsViewModel>($@"
            SELECT ID, OrderNum, Carrier, Driver, Truck, StartTime, EndTime, StartOdometer, EndOdometer, Mileage, Type, Locked
            FROM dbo.fnMileageLog(isnull('{StartDate}', getdate()), coalesce('{EndDate}', '{StartDate}', getdate()), {filterCarrierID}, {filterDriverID}, {filterTruckID}) ml
            WHERE ('{filterType}' LIKE 'Both' OR ml.Type LIKE '{filterType}%')
            AND ({id} = 0 or {id} = ml.ID)
            ORDER BY Truck, StartTime DESC 

这很好地加载到网格中。但是,我注意到当我的替代行(代表卡车的“死头”因此没有订单号)存在(未过滤掉)时,编辑一行需要很长时间。经过检查,事实证明,每次我为所有空 id 条目点击保存时,网格都试图“创建”一条新记录。我测试过的一页上有 43 个。我在网格数据源 requestStart 事件中验证了这一点。显然,在这种情况下,您无法阻止默认值。我如何防止网格这样做?我不反对 hacky 方法。

标签: kendo-grid

解决方案


I got in touch with Telerik and they told me that Telerik internally does an IsNew() check on each model instance (each row) of the dataset. Since my alternating rows are not editable and are only there to identify empty miles driven, the solution was to simply modify the sql query to populate the null IDs with a fake value. We chose -1.

 var data = db.Database.SqlQuery<OdometerLogsViewModel>($@"
        SELECT CASE WHEN ID IS NULL THEN -1 ELSE ID END ID , OrderNum, Carrier, Driver, Truck, StartTime, EndTime, StartOdometer, EndOdometer, Mileage, Type, Locked
        FROM dbo.fnMileageLog(isnull('{StartDate}', getdate()), coalesce('{EndDate}', '{StartDate}', getdate()), {filterCarrierID}, {filterDriverID}, {filterTruckID}) ml
        WHERE ('{filterType}' LIKE 'Both' OR ml.Type LIKE '{filterType}%')
        AND ({id} = 0 or {id} = ml.ID)
        ORDER BY Truck, StartTime DESC 

推荐阅读