2

How do you make a custom command update the telerik grid? For example below I have a button that updates the Last Name for the row to Peters via a custom command button. The action method is called, but the grid is not updated.

@Html.Telerik()
    .Grid(Model)
    .Name("Grid")
    .Columns(columns =>
    {
        columns.Bound(o => o.FirstName);
        columns.Bound(o => o.LastName);
        columns.Bound(o => o.Phone);
        columns.Bound(o => o.State);
        columns.Command(commands => commands.Custom("Stuff")
                                        .Text("Change Name")
                                        .DataRouteValues(route => route.Add(o => o.LastName)
                                                            .RouteKey("lastName"))
                                        .Ajax(true)
                                        .Action("Stuff", "Home"));
    })
    .DataBinding(dataBinding =>
    {
        dataBinding.Server().Select("Index", "Home", new { ajax = ViewData["ajax"] });
        dataBinding.Ajax().Select("_Index", "Home").Enabled((bool)ViewData["ajax"]);
    })
    .Scrollable(scrolling => scrolling.Enabled((bool)ViewData["scrolling"]))
    .Sortable(sorting => sorting.Enabled((bool)ViewData["sorting"]))
    .Pageable(paging => paging.Enabled((bool)ViewData["paging"]))
    .Filterable(filtering => filtering.Enabled((bool)ViewData["filtering"]))
    .Groupable(grouping => grouping.Enabled((bool)ViewData["grouping"]))
    .Footer((bool)ViewData["showFooter"]);

Action Method:

[GridAction]
public ActionResult Stuff(string lastName)
{
    IEnumerable<Person> persons = GetPersons();

    persons.First(p => p.LastName == lastName).LastName = "Peters";


    return View(new GridModel(persons));
}
amit_g
  • 30,880
  • 8
  • 61
  • 118
ctrlalt313373
  • 3,935
  • 7
  • 36
  • 40

1 Answers1

2

Add to ClientEvents grid.

.ClientEvents(events => events.OnComplete("onComplete"))

Add javascript to rebuild the grid if action is Stuff:

@{ Html.Telerik().ScriptRegistrar().OnDocumentReady(@<text> 
function onComplete(e) {
   if (e.name == "Stuff") {
        var $grid = $("#Grid").data("tGrid");
        $grid.rebind();
   }
}
</text>); }
andreydruz
  • 110
  • 1
  • 9