This is a migrated thread and some comments may be shown as answers.

How to pass selected rows from a grid custom toolbar button to MVC method

2 Answers 709 Views
Grid
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 31 Aug 2012, 02:30 PM
I'm trying to implement a custom command on a grid that needs to invoke an MVC method that accepts the currently selected rows in the grid as an argument (probably as an IEnumerable<>).  The method would in turn create a view based on the selections.

I have no problem defining the toolbar button, or even setting .Action() to the MVC method I want to call. I just don't know how to tell Action to send the data I need.

I've also tried implement the onclick method on that button.  While i can ultimately invoke onclick, once there nothing i've tried results in the outcome i want.

I'm using the grid in Ajax mode.

Any suggestions?

2 Answers, 1 is accepted

Sort by
0
Tomaz
Top achievements
Rank 1
answered on 02 Sep 2012, 07:13 PM
Hi,

I have like this to open form
on grid:
.ToolBar(commands => commands.Custom().Text(KOResources.Resources.Add).Url("#").HtmlAttributes(new { @class = "edit-article-add-button" }))


java script:
$(document).ready(function () {
 
        $(".edit-article-add-button").click(function () {
 
        var gridArticle = $("#gridArticleList").data("kendoGrid");
        var IdArticle = 0;
 
        gridArticle.select().each(function () {
            var dataItem = gridArticle.dataItem($(this));
            IdArticle = dataItem.IdArticle;
        });
 
        var windowEditArticle = $('#windowEditArticle').data("kendoWindow");
 
        windowEditArticle.refresh({
            url: "/News/ArticleEdit/",
            data: { id: IdArticle }
        });
 
        windowEditArticle.center();
        windowEditArticle.open();
 
        })
 
    })
0
David
Top achievements
Rank 1
answered on 04 Sep 2012, 05:48 PM
Thanks.  That pointed me in the general direction.  I used the following client side code.  The main difference here is grabbing all of the selected items.
$("#multi_edit").click(function (e) {
    e.preventDefault();
    var grid = $("#Grid").data("kendoGrid");
    var selection = [];
 
    grid.select().each(
        function () {
            var dataItem = grid.dataItem($(this));
            selection.push(dataItem);
        }
    );
 
    $.ajax({
        type: "POST",
        url: "/Item/MultiEdit",
        data: JSON.stringify({ items: selection }),
        dataType: "html",
        contentType: "application/json; charset=utf-8",
        success: function (form) {
            document.open();
            document.write(form);
            document.close();
        }
    });
});

On the controller side, I used the following:

[HttpPost]
public ActionResult MultiEdit(IEnumerable<ItemModel> items) {
    MultiEditItemModel multiEditModel = new MultiEditItemModel()
    {
        Item = items.FirstOrDefault(),
        Count = items.Count()
    };
    Session["MultiEditContext"] = items;
    return View("MultiEdit", multiEditModel);
}

The ModelBinder seemed to have no trouble providing me the incoming JSON data.

I kept some of the context in Session.  When the editor page was submitted, I retreive the saved information so i can get the list of other items also being edited by the one edit session.  I couldn't figure out a suitable way of doing so without hiding it on the page somewhere which seems a bit of a performance hit.
Tags
Grid
Asked by
David
Top achievements
Rank 1
Answers by
Tomaz
Top achievements
Rank 1
David
Top achievements
Rank 1
Share this question
or