Refresh after update Telerik Kendo Grid (MVC)

22,876

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>();
    }
Share:
22,876
Yatiac
Author by

Yatiac

Updated on June 12, 2020

Comments

  • Yatiac
    Yatiac almost 4 years

    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.

  • Yatiac
    Yatiac almost 10 years
    Thank you for your answer, I will try it and post back my results.
  • Yatiac
    Yatiac almost 10 years
    Hello, In the last line (comment 3) i think you meant updatedRecords.ToDataSourceResult instead of ModelState.ToDataSourceResult. Either way it doesn't work. I also tried the same example that you posted and nothing.