简体   繁体   English

ASP.NET MVC Kendo Grid 如何从 javascript 调用 controller 方法

[英]ASP.NET MVC Kendo Grid how to call controller method from javascript

Because I have a custom modal confirmation popup, I'll need to call the the method .Destroy("Remove", "Attachment") from javascript.因为我有一个自定义模式确认弹出窗口,我需要从 javascript 调用方法.Destroy("Remove", "Attachment") How do I call the Remove method from javascript?如何从 javascript 调用Remove方法? I've indicated in the code how to call where I'd like to be able to call the method.我已经在代码中指出了how to call我希望能够调用该方法的位置。 Also, how to pass through the OrderViewModel ?另外,如何通过OrderViewModel

Here's my grid:这是我的网格:

@(Html.Kendo().Grid<TelerikAspNetCoreApp7.Models.OrderViewModel>()
        .Name("grid")
        .Columns(columns =>
        {
            columns.Bound(p => p.OrderID).Filterable(false);
            columns.Bound(p => p.Freight);
            columns.Bound(p => p.OrderDate).Format("{0:MM/dd/yyyy}");
            columns.Bound(p => p.ShipName);
            columns.Bound(p => p.ShipCity);
            columns.Command(command =>
            {
                command.Custom("Destroy")
                    .Click("showDeleteConfirmation")
                    .HtmlAttributes(new { style = "width:40%" });
            }).Width("15%");
        })
        .Pageable()
        .Sortable()
        .Scrollable()
        .Filterable()
        .HtmlAttributes(new { style = "height:550px;" })
        .DataSource(dataSource => dataSource
            .Ajax()
            .PageSize(20)
            .Destroy("Remove", "Attachment")
            .Read(read => read.Action("Orders_Read", "Grid"))
            .Destroy(read => read.Action("Orders_Read", "Grid"))
        )
)

The modal:模态:

@(Html.Kendo()
        .Dialog()
        .Name("DeleteConfirmation")
        .Modal(true)
        .Title("Confirm Delete")
        .Content("Are you sure you want to delete this item?")
        .Visible(false)
        .Actions(a =>
        {
            a.Add().Text("No").Action("cancelDelete");
            a.Add().Text("Yes").Action("confirmDelete").Primary(true);
        })
)

The scripts:脚本:

<script>
    var modelToDelete;

    function showDeleteConfirmation(e) {
        e.preventDefault();
        var grid = $("#grid").data("kendoGrid");
        var dialog = $('#DeleteConfirmation').data("kendoDialog");

        modelToDelete = grid.dataItem($(e.target).parents('tr'));
        dialog.content("Are you sure you want to delete this item with ID - " + modelToDelete.OrderID + "?");
        dialog.open();
    }

    function confirmDelete(e) {
        //how to call .Destroy("Remove", "Attachment") from here
    }

    function cancelDelete() {
    }
</script>

The controller: controller:

public ActionResult Remove([DataSourceRequest] DataSourceRequest request, OrderViewModel attachmentVm)
{
    Attachment attachment = _db.Attachments.FirstOrDefault(o => o.Guid == attachmentVm.Guid);
    attachment.IsActive = false;
    attachment.LastUpdated = DateTime.Now;
    attachment.LastUpdatedBy = _sessionUser.Username;
    _db.SaveChanges();

    return Json(ModelState.ToDataSourceResult());
}

Here's the answer:这是答案:

function confirmDeleteAttach(e) {
    $.ajax({
        url: '/Attachment/Remove',
        data: { Guid: modelToDeleteAttach.Guid },
        type: "POST",
        success: function () {
            gridToDeleteAttach.dataSource.remove(modelToDeleteAttach);
            $('#DeleteConfirmationAttach').data("kendoDialog").close();
        }
    });

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM