简体   繁体   中英

How to delete a row from a Kendo Grid with confirmation and passing in the selected ID into the Destroy method

How can I delete a row from a Kendo Grid with confirmation and passing in the selected ID into the Destroy method? Here's my cshtml code:

    @(Html.Kendo().Grid<EmailTemplateEditor.Models.EmailTemplate>()
    .Name("EmailTemplates")
    .Columns(columns =>
    {
        columns.Bound(c => c.ID).ClientTemplate("<a href='/EmailTemplate/EmailTemplate/#=ID#'>#=ID#</a>");            
        columns.Bound(c => c.Name);
        columns.Bound(c => c.Subject);
        columns.Bound(c => c.OperationalBrand);
        columns.Bound(c => c.Body);
        columns.Bound(c => c.DateAdded).Format("{0:MM/dd/yyyy}");
        columns.Command(command => command.Destroy()).Width(110);
    })
        .HtmlAttributes(new { style = "height: 700px;" })
        .Scrollable()
        .Groupable()
        .AutoBind(true)
        .Sortable()
        .Filterable()
        .Pageable(pageable => pageable
            .PageSizes(true)
            .ButtonCount(5))
        .DataSource(dataSource => dataSource
                .Ajax()
                .Model(model => model.Id(p => p.ID))
                        .Read(read => read.Action("GetEmailTemplates", "Home"))                            
                .PageSize(20)
                .ServerOperation(true)                    
                .Destroy(update => update.Action("Delete", "Home", Model.ID))
        )
)

And here is my controller method:

[HttpPost]
    public ActionResult Delete([DataSourceRequest] DataSourceRequest request, int ID)
    {
        Worker.DeleteEmailTemplate(ID);
        return View();
    }

When I run this, I get an error saying that Model.ID is null. If I can't access the current row's ID in that way, how can I do it? I'd really like to avoid having to refactor this to use a "transport" style of datasource, but if that's the only way to do it, please let me know. Here's what the UI looks like:

UI截图

Thanks

Change your Destroy command to the following.

.Destroy(update => update.Action("Delete", "Home"))

Then your controller to:

public ActionResult Delete([DataSourceRequest] DataSourceRequest request, EmailTemplate myModel)

Then you can access the id using

myModel.ID

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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