Telerik MVC网格ajax删除与客户端事件

时间:2013-08-09 08:49:00

标签: asp.net-mvc-3 telerik-grid telerik-mvc

我正在尝试使用telerik的ajax删除事件从telerik mvc网格中删除记录。 我也定义了onDelete客户端事件,在那里我验证了很少的东西,并且基于我想要删除记录,我的客户端onDelete函数是这样的

function onDelete(e) {
    if (confirm("Are you sure you want to delete?")) {

        $.getJSON('@Url.Action("GetStatus", "MyController")', { UserId: e.dataItem.UserId}, function (status) {
            if (status == 0) {
                return true;
            }
            else if (status == 1) {
                return confirm("Status 1, sure you want to delete?");
            }
            else {
                alert("Status 2, cannot be deleted");
                return false;
            }
        });
    }
    else {
        return false;
    }
}

在网格方面我有类似的东西

.DataBinding(dataBinding => dataBinding.Ajax()
.Delete("_Delete", "MyController"))
.ClientEvents(events => events.OnDelete("onDelete"))

问题是如果我选择取消到第一次确认,它不会删除记录。但是,如果我说删除可以,那么它不会等待进一步的验证(我正在通过ajax响应)并删除记录。

如果我只是放return truereturn false就可以了。

似乎它没有等待ajax响应。有什么工作吗?

1 个答案:

答案 0 :(得分:0)

对于此要求,您必须手动管理此事。

请尝试使用以下代码段。

查看

Mark Up

@(Html.Telerik().Grid<TelerikMvcApplication1.Models.Order>()
    .Name("Grid")
    .Columns(columns =>
    {
        columns.Bound(o => o.OrderID);
        columns.Bound(o => o.OrderName);
        columns.Bound(o => o.OrderID)
           .ClientTemplate("<a onclick='DeleteOrder(<#= OrderID #>)' class='t-button'>Delete</a?")
           .Title("Picture");

    })
    .DataBinding(dataBinding => dataBinding.Ajax().Select("_AjaxBinding", "Home"))
    .Pageable()
    .Sortable()
    .Scrollable()
    .Groupable()
    .Filterable()
)

JS

function DeleteOrder(OrderID) {
    if (confirm("Are you sure you want to delete?")) {
        $.getJSON('@Url.Action("GetStatus", "Home")', { OrderID: OrderID }, function (status) {
            if (status == 0) {
                ConfriMDelete(OrderID);
            }
            else if (status == 1) {
                if (confirm("Status 1, sure you want to delete?")) {
                    ConfriMDelete(OrderID);
                }
                else {
                    return false;
                }
            }
            else {
                alert("Status 2, cannot be deleted");
                return false;
            }
        });
    }
    else {
        return false;
    }
}

function ConfriMDelete(OrderID) {
    $.getJSON('@Url.Action("_Delete", "Home")', { OrderID: OrderID }, function (status) {
        var grid = $("#Grid").data("tGrid");
        grid.ajaxRequest();
    });
}

<强>控制器

    [GridAction]
    public ActionResult _AjaxBinding()
    {
        return View(new GridModel<Order>
        {
            Data = GetOrders()
        });
    }

    protected List<Order> GetOrders()
    {
        List<Order> orders = new List<Order>();

        for (int i = 0; i < 5; i++)
        {
            Order o1 = new Order();
            o1.OrderID = i;
            o1.OrderName = "Name" + i;
            orders.Add(o1);
        }

        return orders;
    }

    public ActionResult GetStatus(int OrderID)
    {
        //your code
    }

    public ActionResult _Delete(int OrderID)
    {
        // perform delete

        return Json(new { status = 1 });
    }