【问题标题】:Kendo grid delete button passing the details of the row剑道网格删除按钮传递行的详细信息
【发布时间】:2015-09-02 08:48:37
【问题描述】:

我有一个剑道网格,我想在其中形成删除按钮以将行的详细信息传递给控制器​​(因为我正在使用 mvc)。然后我将使用这些详细信息从数据库中删除该行。

我在 kendo 网站上搜索,找到了 delete 或 destroy 调用的部分,但我不知道如何将行的这些详细信息传递给控制器​​。

@(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.ProductViewModel>()    
.Name("Grid")    
.Columns(columns => {        
    columns.Bound(p => p.ProductName);
    columns.Bound(p => p.UnitPrice).Width(140);
    columns.Bound(p => p.UnitsInStock).Width(140);
    columns.Bound(p => p.Discontinued).Width(100);
    columns.Command(command => command.Destroy()).Width(110);
})
.ToolBar(toolbar => {
    toolbar.Create();
    toolbar.Save();        
})
.Editable(editable => editable.Mode(GridEditMode.InCell))
.Pageable()
.Navigatable()
.Sortable()
.Scrollable()
.DataSource(dataSource => dataSource        
    .Ajax()         
    .Batch(true)
    .PageSize(20)
    .ServerOperation(false)                
    .Events(events => events.Error("error_handler"))
    .Model(model => model.Id(p => p.ProductID))
    .Create("Editing_Create", "Grid")
    .Read("Editing_Read", "Grid")
    .Update("Editing_Update", "Grid")
    .Destroy("Editing_Destroy", "Grid")
)
)

我的网格:

<div id="grid">

   @(Html.Kendo().Grid<dynamic>()
.Name("BrowseGrid")
.Columns(columns =>
{
    foreach (System.Data.DataColumn c in Model.Grid.Columns)
    {
        columns.Bound(c.ColumnName).EditorTemplateName("String");

    }
    columns.Command(command =>
    {
        command.Destroy();
        command.Custom("").Text("editteam").HtmlAttributes(new { id = "editteam", onClick = "editRow()" });

    });
})

.Scrollable()
.DataSource(dataSource => dataSource
    .Ajax()
    .Events(events => events.Error("error_handler"))
    .Model(model =>
    {
        //Define the model
        foreach (System.Data.DataColumn column in Model.Grid.Columns)
        {
            model.Field(column.ColumnName, column.DataType);
            model.Id("Id");
        }
    })
    .Read(read =>

        read.Action("BrowseGrid", "Configuration")
    )
    .Destroy( update => update.Action("Delete", "Configuration"))

    //Data("{nodeID:"+@Model.nodeID+"}"
)

        .Pageable(pageable => pageable
             .Refresh(true)

        .PageSizes(new int[] { 10, 100, 1000, 10000, 100000, 1000000 })
        .ButtonCount(10)
    )
  )

【问题讨论】:

    标签: asp.net-mvc button kendo-ui kendo-grid delete-row


    【解决方案1】:

    请改成:

    .Destroy(update => update.Action("Process_Destroy", "controller name"))
    

    在控制器中,

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Process_Destroy([DataSourceRequest] DataSourceRequest request, ProductViewModel product)
    {
        if (product != null)
        {
            //write your code for delete action;
        }
    
        return Json(ModelState.ToDataSourceResult());
    }
    

    这会起作用。

    【讨论】:

    • 谢谢,但它仍然无法工作问题是我的代码与剑道网站中的网格不完全一样。我从一个模型中的数据表填充网格,所以我想在按下删除时选择一行并将其传递给控制器​​,然后我可以从那里删除它。我的代码已添加到问题中
    • .Editable(editable => editable.Mode(GridEditMode.InLine))
    • 我收到的模型是一个空模型,数据表中没有任何内容,因此没有任何内容传递给控制器​​
    【解决方案2】:

    我假设您只需要该行的 id 即可将其删除,但我认为您可以发送所选行的完整模型。我都发了。

    像这样:

    .Destroy(d => d.Action("controllerMethod", "controllerName").Data("jsFunction"))
    

    在 javascript 中:

    function jsFunction() {
        var grid = $("#BrowseGrid").data("kendoGrid");
        var id = grid.dataItem(grid.select()).Id;
        return {
            id: id
        };
    }
    

    你的控制器方法应该接收参数:

    public ActionResult controllerMethod(string id)
    

    【讨论】:

    • 谢谢,我这样做了,但由于未知原因,我添加了 .Data("jsFunction") 部分会影响整个视图,并且网格没有响应数据源以及它下面的组合框有些改变了!你知道其中的原因吗?
    • 如果您删除了.Data("jsFunction") 部分,这不会发生吗?
    • 不,如果它被删除,网格会显示得很好,所有的东西都会出现在其中,即使是删除按钮,虽然它们不起作用
    • 这不可能,肯定是有其他问题。除了 Read 方法,您可以尝试相同的方法吗?看它在读取时是否向控制器发送数据?我这样做并且它有效,我只是想看看那里的错误在哪里。更改 jsFunction 并发送一个微不足道的值,例如 return { test: 1 }; 并在控制器的 read 方法中捕获参数 test。此外,如果您可以添加更多代码、网格定义和 javascript(例如,在 fiddle 链接中)
    • 它做了同样的事情!使用部分视图会导致这种情况吗?这是更多的代码希望它有用。 jsfiddle.net/mf184nzf
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多