2013-05-13 73 views
3

我有一個Telerik MVC Grid,我想在刪除一個項目後重新生成grid。Telerik MVC Grid,ajax從定製命令中刪除後重新綁定?

這裏是我的網格:

@(Html.Telerik().Grid(Model.Item).Name("Items").Sortable().Scrollable(x => x.Height(400)).Filterable().Pageable(x => x.PageSize(20)) 
.Pageable() 

.Columns(columns => 
{ 
    columns.Bound(x => x.Equipment.Location.Building.Name).Title("Building"); 
    columns.Bound(x => x.Equipment.Location.Room).Width(150); 
    columns.Bound(x => x.Number).Title("Number").Width(150); 
      columns.Command(commands => 
      { 
       if (Model.CanViewHistory) 
       { 
        commands 
        .Custom("ViewHistory") 
        .Text("History") 
        .ButtonType(GridButtonType.Text) 
        .SendState(false) 
        .DataRouteValues(x => x.Add(y => y.Id).RouteKey("id")) 
        .Action("Index", "ItemHistory"); 
       } 

       if (Model.CanEdit) 
       { 
        commands 
        .Custom("Edit") 
        .Text("Edit") 
        .ButtonType(GridButtonType.Image).ImageHtmlAttributes(new { @class = "t-icon t-edit t-test" }) 
        .DataRouteValues(x => x.Add(y => y.Id).RouteKey("id")) 
        .SendState(false) 
        .Action("Save", "Items"); 

        commands 
        .Custom("Delete").HtmlAttributes(new { onclick = "return confirm('Are you sure you want to delete this item?')" }) 
        .Text("Delete").Ajax(true) 
        .ButtonType(GridButtonType.Image).ImageHtmlAttributes(new { @class = "t-icon t-delete t-test" }) 
        .DataRouteValues(x => x.Add(y => y.Id).RouteKey("id")) 
        .SendState(false) 
        .Action("Delete", "Items", new { Area = "Apps" }); 
       } 

      }).Title("Actions"); 
}).ClientEvents(events => events.OnComplete("onComplete"))) 

而我的方法刪除執行後打電話到的是:

<script type="text/javascript"> 
    function onComplete() { 
     $("#Items").data("tGrid").rebind(); 
    } 
</script> 

操作:

public ActionResult Delete(Guid id) 
    { 
     Item item = _itemService.GetOne(x => x.Id == id); 

     _itemService.Delete(item); 

     return RedirectToAction("Index"); 
    } 

行動工程,該項目實際上並被刪除,但網格不刷新,只有在手動重新加載頁面後,刪除的項目纔會消失。在我的控制檯中,當我單擊刪除按鈕時,出現以下錯誤:

Uncaught ReferenceError: xhr is not defined telerik.grid.min.js:1 

我在做什麼錯?

編輯:使用下面的基里爾的方法刪除我的錯誤,但網格仍然不刷新,javascript被成功調用並獲得rebind()命令。

回答

7

您不應該從此方法返回ViewResult。你應該返回JsonResult。

public JsonResult Delete(Guid id) 
{ 
    try 
    { 
     Item item = _itemService.GetOne(x => x.Id == id); 

     _itemService.Delete(item);   

     return Json(new { result = true }); 
    } 
    catch 
    { 
     return Json(new { result = false }); 
    } 
} 

而且的onComplete應該是:

function onComplete(e) { 
    if (e.name == "Delete") { 
     var result = e.response.result; 
     if(result==true) 
      $("#Items").data("tGrid").rebind(); 
     else{ 
      alert("Error on deleting") 
     } 
    } 
} 

UPDATE 這適用於阿賈克斯結合。

@(Html.Telerik().Grid<ItemType>.Name("Items") 
      .Sortable().Scrollable(x => x.Height(400)) 
      .Filterable().Pageable(x => x.PageSize(20)) 
//you should add this line: 
      .DataBinding(dataBinding => dataBinding.Ajax().Select("Select", "Items")) 
      .Columns(columns => 
      { 
       columns.Bound(x => x.Equipment.Location.Building.Name).Title("Building"); 
       columns.Bound(x => x.Equipment.Location.Room).Width(150); 
       columns.Bound(x => x.Number).Title("Number").Width(150); 
       columns.Command(commands => 
       { 
        if (Model.CanViewHistory) 
        { 
         commands.Custom("ViewHistory") 
           .Text("History") 
           .ButtonType(GridButtonType.Text) 
           .SendState(false) 
           .DataRouteValues(x => x.Add(y => y.Id).RouteKey("id")) 
           .Action("Index", "ItemHistory"); 
        } 

        if (Model.CanEdit) 
        { 
         commands.Custom("Edit") 
           .Text("Edit") 
           .ButtonType(GridButtonType.Image) 
           .ImageHtmlAttributes(new { @class = "t-icon t-edit t-test" }) 
           .DataRouteValues(x => x.Add(y => y.Id).RouteKey("id")) 
           .SendState(false) 
           .Action("Save", "Items"); 

         commands.Custom("Delete") 
           .HtmlAttributes(new { onclick = "return confirm('Are you sure you want to delete this item?')" }) 
           .Text("Delete") 
           .Ajax(true) 
           .ButtonType(GridButtonType.Image) 
           .ImageHtmlAttributes(new { @class = "t-icon t-delete t-test" }) 
           .DataRouteValues(x => x.Add(y => y.Id).RouteKey("id")) 
           .SendState(false) 
           .Action("Delete", "Items", new { Area = "Apps" }); 
        } 

        }).Title("Actions"); 
       }) 
    .ClientEvents(events => events.OnComplete("onComplete"))) 

,你應該加入行動獲得的數據網格:

[GridAction] 
public JsonResult GetChangeHistory(Guid stockCompanyId) 
{ 
    IEnumerable<ItemType> items = ... //code to get items. 
    return Json(new GridModel<ItemType>(items)); 
} 

我承擔的項目集合的這個元素類型的ItemType的。

+0

此解決方案刪除了​​錯誤,但網格仍然沒有重新綁定,我需要刷新頁面才能看到該項目消失。我是否必須將數據綁定設置爲ajax並在網格的剃鬚刀代碼中提供一種方法? – SventoryMang 2013-05-20 15:53:32

+0

是的,這與ajax綁定一起工作。 – 2013-05-21 04:33:02

+0

你是怎麼做到的?你能舉一個例子嗎?我粘貼了我的整個網格代碼。 – SventoryMang 2013-05-21 20:00:44

相關問題