2017-02-11 55 views
0

当我加载网页,它是一个局部页面,网格被填充有数据剑道网格数据未结合正确刷新后

enter image description here

但一旦我称这样

刷新方法
<button onclick="doRefresh">refresh</button> 
<script type="text/javascript"> 
    var doRefresh = function() { 
      var kendogrid = $("#this_Id").data("kendoGrid");     
      kendogrid.refresh(); 
    } 
</script> 

它应该刷新网格,使用当前绑定到网格的数据源来重建它。但网格变为空

enter image description here 我想我绑定模型错了,因为一旦我刷新网格我失去所有的数据。我试图绑定模型并使用bindto方法作为mentioned here

这是我的观点:

@model List<bModel> 

@(Html.Kendo().Grid(Model) 
     .Name("this_Id") // template expression, to be evaluated in the master context 
    .Columns(columns => 
    { 
     columns.Bound(m => m.Sequence).Title("Seq."); 
     columns.Bound(m => m.JobType).Title("Job Type"); 
     columns.Bound(m => m.Service).Title("Service"); 
     columns.Bound(m => m.Status).Title("Status"); 
     columns.Bound(m => m.PropertyType).Title("Property Type"); 
     columns.Bound(m => m.PropertySubType).Title("Property SubType"); 
     columns.Bound(m => m.Address).Title("Address"); 
    }) 
    .Sortable() 
    .Selectable(s => s.Mode(GridSelectionMode.Multiple).Type(GridSelectionType.Row)) 
    .ClientDetailTemplateId("taskTemplate") 
    .DetailTemplate(
       ....... 
     ) 
    .DataSource(dataSource => dataSource 
     .Server() 
     .Model(model => model.Id(jobItem => jobItem.Id)) 
    ) 
    .Read(read => read.Action("GetJobForBulkUpdate", "Project", new { jobId = "#=Id#" })) 
    .Events(events => events.DetailExpand("detailExpand")) 
) 

而且我打电话面板栏的视图这样

@(Html.Kendo().PanelBar() 
     .Name("panel-bar") 
     .ExpandMode(PanelBarExpandMode.Multiple) 
     .ExpandAll(true) 
     .Items(panel => 
     {  
      panel.Add() 
       .Text("<span id='batch-update-panel-bar-job'>JOB-TASK</span>") 
       .Encoded(false) 
       .Content("<div id='update-model'>" + Html.Action("BatchUpdateJob", "BatchUpdate", new { projectId = Model.Id}).ToHtmlString() + "</div>");  
     }) 
) 

和控制方法是这样的

public ActionResult BatchUpdateJob(int projectId) 
{ 
    mymodel = something; 
    return PartialView("_MymodelPageJob", mymodel); 
} 

public ActionResult GetJobForBulkUpdate(int projectId, [DataSourceRequest] DataSourceRequest request) 
    {    
      List<OfData> jobData = getData(); 
     return Json(jobData.ToDataSourceResult(request)); 
    } 
+0

@ SeanCh抱歉,周末很忙。 –

回答

0

更新:用户更改绑定到Ajax和问题是dat被返回的格式不正确。通过遵循下面的ajax格式示例修复传入的数据格式有助于解决该问题。

一旦我调用像这样的网格变成空的刷新方法。 kendogrid.refresh();

使用正确Html.Action不知道,如果你的控制器被击中

@Html.Action("BatchUpdateJob", "ControllerName", new { projectId = Model.Id }); 

只是呼吁格刷新无数据,当然它会显示为空。因此,您希望基于特定的setTimeout自动刷新网格,因为目标数据在特定时间间隔后可能会有所不同。

请勿使用此软件!你正在做它在一个错误的方式看到文档和清除基础

var kendogrid = $("#this_Id").data("kendoGrid"); 
    console.log(kendogrid.dataItem()); 
    kendogrid.refresh(); 

使用它代替 -

//read will request the server and reload only reload datasource 
    $('#this_Id').data('kendoGrid').dataSource.read(); 

    //refresh will re-render items in grid from the current datasource 
    $('#this_Id').data('kendoGrid').refresh(); 

现在 ---- AS用户我增加了要求样品演示AJAX服务器端绑定

AJAX绑定:Ajax binding

视图模型

public class ProductViewModel 
    { 
     public int ProductID { get; set; } 
     public string ProductName { get; set; } 
     public short? UnitsInStock { get; set; } 
    } 

电网在剃刀语法这里讨论的Telerik的文档

@(Html.Kendo().Grid<KendoGridAjaxBinding.Models.ProductViewModel>() 
     .Name("grid") 
     .DataSource(dataSource => dataSource 
      .Ajax() 
     //Set the action method which will return the data in JSON format.  
      .Read(read => read.Action("Products_Read", "Home")) 

     ) 
     ) 
     .Columns(columns => 
     { 
      columns.Bound(product => product.ProductID); 
      columns.Bound(product => product.ProductName); 
      columns.Bound(product => product.UnitsInStock); 
     }) 
     .Pageable() 
     .Sortable() 
) 

控制器

public ActionResult Products_Read([DataSourceRequest]DataSourceRequest request) 
     { 
      using (var northwind = new NorthwindEntities()) 
      { 
       IQueryable<Product> products = northwind.Products; 
       //Convert the Product entities to ProductViewModel instances. 
       DataSourceResult result = products.ToDataSourceResult(request, product => new ProductViewModel 
         { 
         ProductID = product.ProductID, 
         ProductName = product.ProductName, 
         UnitsInStock = product.UnitsInStock 
         }); 
       return Json(result); 
      } 
     } 

服务器端绑定:作为Telerik的文档这里讨论Server side binding

有几个方法可以做到这一点:

  • 绑定到视图模型
  • 绑定到ViewData中的项目或ViewBag使用
  • BindTo方法传递额外数据的操作方法

这里是GitHub的解决方案是讨论服务器端绑定下载它 ,看看你错过了什么 - Server side binding

+0

在数据源上调用读取会导致页面重新加载。由于我的网格使用服务器数据绑定,所以我认为应该这样做。但这不是我想要的,因为它看起来像我没有正确地绑定模型,这就是为什么Grid没有任何东西,因为它是数据源对象。 –

+0

我明白我做错了什么。我将它更改为AJAX,但当我检查数据源时,我确实看到了数据,但调用dataitem方法返回了空值。更多地看待它。 –

+0

感谢您的回复。我已经阅读了服务器端绑定页面。我在我的问题中发布了链接。我对这些混乱的代码表示歉意,我遗漏了一些东西,并重新命名了一些其他东西,因为这是我的工作地点的代码。 –