簡體   English   中英

Telerik MVC Grid Ajax刪除操作

[英]Telerik MVC Grid Ajax delete action

我有一個網格,我希望能夠刪除行而不必從控制器操作中返回新的結果集。

我認為:

@(Html.Telerik().Grid<InterestTopicViewModel>()
          .Name("Grid")
          .DataKeys(keys => keys.Add(x => x.Id))
          .ToolBar(commands => commands.Insert().ButtonType(GridButtonType.Image).ImageHtmlAttributes(new {style="margin-left:0"}))
          .DataBinding(dataBinding => dataBinding.Ajax()
                                         .Select("Select", "InterestTopic")
                                         .Insert("Insert", "InterestTopic")
                                         .Update("Update", "InterestTopic")
                                         .Delete("Delete", "InterestTopic"))
          .Columns(columns =>
                      {
                         columns.Bound(x => x.Name);
                         columns.Command(commands =>
                                            {
                                               commands.Edit().ButtonType(GridButtonType.Image);
                                               commands.Delete().ButtonType(GridButtonType.Image);
                                            }).Title("Actions");
                      })
          .Editable(editing => editing.Mode(GridEditMode.InLine))
        )

這是我的控制器中:

  [AcceptVerbs(HttpVerbs.Post)]
  [GridAction]
  public ActionResult Delete(int id)
  {
     InterestTopicViewModel interestTopicViewModel = this.InterestTopicPresenter.GetInterestTopic(id);

     if (this.InterestTopicPresenter.DeleteInterestTopic(id))
        base.LogUserAction(UserActionLoggingType.DeleteInterest, interestTopicViewModel.Name);

     return this.View(new GridModel(this.InterestTopicPresenter.GetList()));
  }

如您所見,在控制器中,我必須在函數末尾返回GridModel對象中的整個列表。 如果我不這樣做,視圖將不會刷新。

是否可以僅在控制器的幫助下刪除記錄,然后讓Telerik刪除javascript中相應行的div?

謝謝。

如果您不擔心javascript和jQuery,這並不是很難。 我通常不使用網格命令列和綁定,而只是使用模板將其自己連接起來,例如(使用Razor語法):

.Columns(columns =>
{
    columns.Bound(x => x.Name);
    columns.Template
    (
        @<text>
            <a href="#" onclick="delete(this, @item.Id);">Delete</a>
        </text>
    );
})

item是Telerik在鍵入您的模型的列模板綁定中提供的字段。 您的刪除功能不需要返回數據。 您可以執行以下操作:

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult Delete(int id)
{
    // call your code to delete the item here
    return Json(new { resultCode = "success" });
}

然后創建一個javascript函數,以發布到您的刪除函數。

function delete(sender, id)
{
    $.ajax({
        type: "POST", // important, only perform deletes on a post
        url: '/delete',
        data: { id: id },
        success: function (result)
        {
            if (result.resultCode == "success")
            {
                var row = $(sender).closest("tr");
                row.remove();
            }
        }
    });
}

這樣的事情。 我不知道確切的語法,但希望這足以讓您入門。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM