Refresh after update Telerik Kendo Grid (MVC)

匆匆过客 提交于 2020-01-23 02:06:36

问题


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.

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

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

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.


回答1:


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

And a Javascript Function:

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

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



回答2:


You are returning the wrong object. I don't really know how you get your data, because you did not posted the GET controller, so I'm going to try to guess it. After you updated your data, you need to send them back to the grid. The ModelState does not contain the data you want. Try this:

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

    var updatedRecords = null;//1

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

            updatedRecords = xgr.EnviromentRepository.ListAll();//2 -- you might need to add "ToList()", depending on your implementation
        }
        xgr.EnviromentRepository.Save(environment);
    }
    return Json(updatedRecords.ToDataSourceResult(request, ModelState));//3
}

Please see this link for a complete example.



来源:https://stackoverflow.com/questions/24141387/refresh-after-update-telerik-kendo-grid-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!