Telerik MVC网格自定义命令更新网格

时间:2012-01-27 17:32:04

标签: c# ajax asp.net-mvc-3 grid telerik-mvc

如何使自定义命令更新telerik网格?例如下面我有一个按钮,通过自定义命令按钮将行的姓氏更新为Peters。调用action方法,但不更新网格。

@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"]);

行动方法:

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

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


    return View(new GridModel(persons));
}

1 个答案:

答案 0 :(得分:2)

添加到ClientEvents网格。

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

如果操作是Stuff:

,添加javascript以重建网格
@{ Html.Telerik().ScriptRegistrar().OnDocumentReady(@<text> 
function onComplete(e) {
   if (e.name == "Stuff") {
        var $grid = $("#Grid").data("tGrid");
        $grid.rebind();
   }
}
</text>); }