更新 Telerik Kendo Grid (MVC) 后刷新 [英] Refresh after update Telerik Kendo Grid (MVC)

查看:18
本文介绍了更新 Telerik Kendo Grid (MVC) 后刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一些环境数据的 Kendo Grid.网格的字段之一是isDefault",它接收 1 或 0(真或假).在数据库中,我有一个触发器,当某些记录设置为 isDefault = 1 时,任何其他记录都会更新为 isDefault = 0,只是为了确保只有一个默认环境.

I have a Kendo Grid with some environments data. One of the fields of the grid is "isDefault" wich recieve 1 or 0 (for true or false). In the database I have a trigger that when some record is set to isDefault = 1 any other record is update to isDefault = 0, just to make sure there is only one default environment.

Kendo 网格工作正常,它绑定数据并更新记录就好了,但更新后,网格不会刷新所有记录,如果有的话,比如说,记录 1 和 isDefault =1 并且我更新记录 4 到 isDefault = 1 触发器被触发并将所有其他记录更新为 isDefault = 0 但网格仍然显示记录 1 和 isDefault = 1 现在记录 4 和 isDefault = 1

The Kendo grid is working fine, it binds the data and updates the records just fine but after the update, the grid is not refreshing all the records and if there was, lets say, record 1 with isDefault =1 and I update record 4 to isDefault = 1 the trigger is fired and updates all others records to isDefault = 0 but the grid still showing record 1 with isDefault = 1 and now record 4 with isDefault = 1

这是我认为的代码:

Html.Kendo().Grid<Models.Environment>()
                   .Name("environmentGrid")
                   .Sortable()
                   .ToolBar(tb =>  tb.Create())
                   .Editable(editable => editable.Mode(GridEditMode.PopUp))
                   .Columns(cols =>
                   {
                       cols.Bound(c => c.Name).Width(150).Sortable(true);
                       cols.Bound(c => c.ConnectionString).Width(150).Sortable(true);
                       cols.Bound(c => c.Template).Width(150).Sortable(true);
                       cols.Bound(c => c.isDefault).Width(150).Sortable(true);
                       cols.Bound(c => c.StatusID).Width(150).Sortable(true);
                       cols.Command(command => { command.Edit();}).Width(60);
                   })
                   .DataSource(ds => ds
                       .Ajax()
                       .Model(model => 
                       { 
                           model.Id(m => m.EnvironmentID);
                       })
                       .Read(r => r.Action("GetEnvironments", "Admin"))
                       .Update(update => update.Action("UpdateEnvironments", "Admin"))
                       .Create(update => update.Action("UpdateEnvironments", "Admin"))                           
                   )

这是我控制器上的代码:

and this is the code on my controller:

[AcceptVerbs(HttpVerbs.Post)]
    public ActionResult UpdateEnvironments([DataSourceRequest] DataSourceRequest dsRequest, Environment environment)
    {
        environment.ModifiedBy = userName;

        if (environment != null && ModelState.IsValid)
        {
            if (environment.EnvironmentID != 0)
            {
                var toUpdate = xgr.EnviromentRepository.ListAll().FirstOrDefault(p => p.EnvironmentID == environment.EnvironmentID);
                TryUpdateModel(toUpdate);
            }
            xgr.EnviromentRepository.Save(environment);
        }
        return Json(ModelState.ToDataSourceResult());
    }

预先感谢您的回答.

推荐答案

我终于让它工作了.添加了一个事件处理程序:

I finally get it to work. Added an event handler:

Html.Kendo().Grid<Models.Environment>()
                   .Name("environmentGrid")
                   .Sortable()
                   .ToolBar(tb =>  tb.Create())
                   .Editable(editable => editable.Mode(GridEditMode.PopUp))
                   .Columns(cols =>
                   {
                       cols.Bound(c => c.Name).Width(150).Sortable(true);
                       cols.Bound(c => c.ConnectionString).Width(150).Sortable(true);
                       cols.Bound(c => c.Template).Width(150).Sortable(true);
                       cols.Bound(c => c.isDefault).Width(150).Sortable(true);
                       cols.Bound(c => c.StatusID).Width(150).Sortable(true);
                       cols.Command(command => { command.Edit();}).Width(60);
                   })
                   .DataSource(ds => ds
                       .Ajax()
                       .Model(model => 
                       { 
                           model.Id(m => m.EnvironmentID);
                       })
                       .Events(events =>
                              {
                                    events.RequestEnd("onRequestEnd"); //I've added this
                              })
                           .Read(r => r.Action("GetEnvironments", "Admin"))
                           .Update(update => update.Action("UpdateEnvironments", "Admin"))
                           .Create(update => update.Action("UpdateEnvironments", "Admin"))                           
                       )

还有一个 Javascript 函数:

And a Javascript Function:

function onRequestEnd(e) {
        if (e.type == "update") {
            $("#environmentGrid").data("kendoGrid").dataSource.read();
        }
    }

我还需要将 EnvironmentRepository 上的 ListAll() 方法修改为如下所示:

Also I needed to modify my ListAll() Method on the EnvironmentRepository to be like this:

 public List<XML_Environment> ListAll()
    {
        _dataContext = new XMLGenEntitiesDataContext(); //I've to add this line. so the context is instantiated every time I call the ListAll Method.
        return _dataContext.XML_Environments.OrderBy<XML_Environment, string>(c => c.EnvironmentName).ToList<XML_Environment>();
    }

这篇关于更新 Telerik Kendo Grid (MVC) 后刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆