繁体   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