【问题标题】:Kendo Grid data not binding correctly after refresh刷新后剑道网格数据未正确绑定
【发布时间】:2017-02-11 02:30:34
【问题描述】:

当我加载页面时,它是一个部分页面,网格中填充了数据

但是一旦我像这样调用刷新方法

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

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

我认为我绑定模型错误,因为一旦我刷新网格,我就会丢失所有数据。我已经尝试绑定模型并使用 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));
    }

【问题讨论】:

  • @SeanCh 抱歉周末很忙。

标签: c# asp.net asp.net-mvc telerik


【解决方案1】:

更新:用户将绑定更改为 Ajax,问题是返回的数据格式不正确。按照下面的 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(); 

现在 ---- 应用户要求,我正在为 AJAX服务器端绑定 添加示例演示。

AJAX BINDING : 如 Telerik 文档 Ajax binding

中所述

视图模型

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

Razor 语法中的网格

  @(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

【讨论】:

  • 对数据源调用 read 会导致页面重新加载。由于我的网格使用服务器数据绑定,因此我认为这是可以预料的。但这不是我想要的,因为看起来我没有正确绑定模型,这就是为什么 Grid 没有任何东西作为它的数据源对象。
  • 我明白我做错了什么。我将其更改为 AJAX,但是当我检查数据源时,我确实看到了数据,但调用 dataitem 方法返回了一个空值。进一步研究。
  • 感谢您的回复。我已经阅读了服务器端绑定页面。我在我的问题中发布了链接。我为混乱的代码道歉,我遗漏了一些东西并重命名了一些其他的东西,因为这是我工作场所的代码。
  • 请更新您的问题。不介意,但请在发帖时考虑周到,因为它需要时间来回答
  • 在阅读了更多内容并查看了 ajax 示例之后,我让它工作了。我返回的数据也是错误的格式。谢谢:D
猜你喜欢
  • 2014-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-24
  • 2015-05-07
相关资源
最近更新 更多