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

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

问题描述

我有一个剑道电网与某些环境中的数据。一个网格的领域之一是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.

剑道网格工作正常,它结合了数据并更新记录得很好,但更新后,网格不刷新所有的记录,如果有,可以说,创纪录的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

这是在我看来,code:

This is the code on my view:

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"))                           
                   )

这是我的控制器上的code:

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());
    }

感谢您提前为你的答案。

Thank you in advance for your answers.

推荐答案

我终于得到它的工作。添加了一个事件处理程序:

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的剑道格后刷新(MVC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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