2012-07-25 67 views
0

我有一個簡單Telerik的網格爲ASP.NET MVC阿賈克斯delete命令。然而,刪除工作,頁面顯示錯誤,並且不會更新。錯誤刪除Telerik的電網運行ASP.NET MVC的

我也想呼籲AJAX調用返回自定義JavaScript,但無法弄清楚在何處放置代碼。

這裏的觀點:

Html.Telerik() 
    .Grid<ScenarioVm>(Model) 
    .Name("scenarioGrid") 
    .DataBinding(dataBinding => dataBinding 
     .Ajax() 
     .Delete("Delete", "Scenario") 
     .Select("Index", "Scenario")) 
    .DataKeys(keys => keys.Add(c => c.Id)) 
    .Columns(columns => 
    { 
     columns.Template(o => o.Name) 
      .Title("Scenario") 
      .FooterTemplate(@<text>Total @Model.Count()</text>); 
     columns.Bound(o => o.IsLocked); 
     columns.Bound(o => o.ContractMonth); 
     columns.Bound(o => o.CreateDate); 
     columns.Command(commands => commands 
       .Delete() 
       .ButtonType(GridButtonType.Image)) 
      .Title("Delete"); 
    }) 
    .Sortable() 
    .Scrollable(scroll => scroll.Height(200)) 
    .ClientEvents(events => events.OnDelete("onDelete"))) 

JavaScript時被AJAX調用之前調用:

function onDelete(e) { 
    var scenario = e.dataItem; 
    if (scenario.CanDelete == false) { 
     alert("Can not delete " + 
      e.dataItem.Name + 
      ": there exists a solution!"); 
     return false; 
    } else { 
     $.blockUI({ 
      css: { 
       border: 'none', 
       padding: '15px', 
       backgroundColor: '#000', 
       '-webkit-border-radius': '10px', 
       '-moz-border-radius': '10px', 
       opacity: .5, 
       color: '#fff' 
      } 
     }); 
     return true; 
    } 
} 

控制器的方法:

[HttpPost] 
[GridAction] 
public ActionResult Delete(Scenario scenario) 
{ 
    Logger.Info("Delete scenario " + scenario); 
    if (scenario == null) 
    { 
     return new EmptyResult(); 
    } 

    try 
    { 
     _scenarioRepository.Delete(scenario); 
     Logger.Info("Successfully deleted " + scenario); 
    } 
    catch(Exception e) 
    { 
     Logger.Error(scenario + e.Message, e); 
     var result = new JsonResult 
     { 
      JsonRequestBehavior = JsonRequestBehavior.DenyGet, 
      Data = new { 
       ErrorCode = 1, 
       ErrorMessage = e.GetType() + ": " + e.Message 
      } 
     }; 

     return result; 
    } 

    return new EmptyResult(); 
} 

回答

1

如果我理解正確的問題,你要當操作完成要執行的JavaScript函數。示例源代碼中的最後一行是定義OnComplete事件的回調。

.ClientEvents(events => events 
      .OnLoad("onLoad") 
      .OnEdit("onEdit") 
      .OnDetailViewCollapse("onDetailViewCollapse") 
      .OnDetailViewExpand("onDetailViewExpand") 
      .OnDelete("onDelete") 
      .OnSave("onSave") 
      .OnDataBinding("onDataBinding") 
      .OnRowDataBound("onRowDataBound") 
      .OnRowSelect("onRowSelect") 
      .OnDataBound("onDataBound") 
      .OnColumnResize("onColumnResize") 
      .OnColumnReorder("onColumnReorder") 
      .OnComplete("onComplete")) 

Telerik documentation還具有一個例子如何定義的onComplete事件。

+0

'.ClientEvents(事件=> events.OnDelete( 「onDelete」)的onComplete( 「的onComplete」))'導致編譯錯誤:編譯器錯誤消息:CS1061: 'Telerik.Web.Mvc.UI.Fluent.GridClientEventsBuilder' 確實不包含'OnComplete'的定義,並且沒有找到接受'Telerik.Web.Mvc.UI.Fluent.GridClientEventsBuilder'類型的第一個參數的擴展方法'OnComplete'(你是否缺少using指令或程序集引用?) – jprusakova 2012-07-26 14:42:40

1

有幾件事情我見上面的代碼錯誤。首先,客戶端事件OnDelete將在之前在客戶端上觸發請求被髮送到服務器。如果您希望將Ajax調用的結果發送到服務器,則需要處理OnComplete事件。此事件在之後觸發,向服務器的Ajax調用完成並將結果返回給客戶端。

二,GridAction行爲過濾希望你IGridAction類型的值返回到您的視圖。由於您沒有返回預期的值,因此會給客戶端帶來錯誤。這裏是你的控制器的方法可能是什麼樣子的例子:

[HttpPost] 
[GridAction] 
public ActionResult Delete(Scenario scenario) 
{ 
    // Delete item and perform other operations as required 
    var data = ... // Get an updated data set, with the deleted item removed 
    var model = new GridModel<ScenarioVm>(data); 
    return View(model); 
} 

下面是一個article討論阿賈克斯細節上Telerik的網站綁定。

我希望有幫助。

+0

我試圖返回'返回視圖(BuildViewModel());'從控制器刪除()方法,它會導致在服務器上的錯誤500。 – jprusakova 2012-07-25 23:59:50

+0

我需要OnDelete和OnComplete事件,以在AJAX調用之前和之後執行代碼。我如何指定OnComplete處理程序? 「ClientEvents(events => events.OnDelete(」onDelete「)。OnComplete(」onComplete「))」會導致語法錯誤。 – jprusakova 2012-07-26 00:04:49

+0

@jprusakova您是否定義了onComplete JavaScript函數? – Tx3 2012-07-26 13:02:00