【问题标题】:Telerik MVC Grid Ajax delete actionTelerik MVC Grid Ajax 删除操作
【发布时间】:2012-07-24 01:48:29
【问题描述】:

我有一个网格,我希望能够删除行,而不必从控制器操作返回新的结果集。

在我看来:

@(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?

谢谢。

【问题讨论】:

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


    【解决方案1】:

    如果你不怕 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 函数以 POST 到您的删除函数。

    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();
                }
            }
        });
    }
    

    类似的东西。我不知道确切的语法,但希望这足以让您入门。

    【讨论】:

    • 这是一个非常好的解决方案。只有一个问题;我的项目中有很多网格,如果我需要实现这样的解决方案,我会为此功能编写大量代码。在关键的地方,我肯定会这样做,但对于其余的,我更喜欢有一个已经在 Telerik 框架中实现的解决方案。
    猜你喜欢
    • 1970-01-01
    • 2011-08-10
    • 1970-01-01
    • 1970-01-01
    • 2012-07-25
    • 2012-12-07
    • 1970-01-01
    • 2013-05-07
    • 1970-01-01
    相关资源
    最近更新 更多