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

[Solved] Passing an array when loading/sorting with ajax binding

2 Answers 34 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Dario
Top achievements
Rank 1
Dario asked on 06 Jun 2012, 12:25 PM
Hi.
When my grid loads or I do something with it (sorting the columns, grouping etc.) I want to pass an array of IDs of Books to the controller. And than the controller should use Linq to SQL to fetch the Books from the database and filter out the Books with IDs that I have passed.

I have tried with: .DataBinding(dataBinding => dataBinding.Ajax().Select("_AjaxBinding", "Controller", new { IDBooks }))
and: using ClientEvent: .OnDataBinding("onDataBinding") and in onDataBinding function pass the array on countless ways but no resault. Eg. 
var $IDBooks = $('input[name="IDBooks"]');
var grid = $("#BooksForBorrowing").data("tGrid");
grid.rebind({ IDBooks: $IDBooks });
or:
var $IDBooks= $('input[name="IDBooks"]');
// pass additional values by setting the "data" field of the event argument
e.data = {
    IDBooks: $IDBooks
};

I was reading all sorts of posts and blogs but nothing helps me.
Is there any way to do this?

2 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 11 Jun 2012, 06:32 AM
Hello Dario,

Basically, you should pass the parameters with names in the format "ActionParameterName[index]" in order to send the array to the action method. For example:

function onDataBinding(e) {
    var idBooks = {};
    $('input[name="IDBooks"]').each(function (index) {
        idBooks["IDBooks[" + index + "]"] = $(this).val();
    });
 
 
    e.data = idBooks;
}
To send the array through the request route values, you can use a RouteValueDictionary with parameters in the same format.


Kind regards,
Daniel
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now.
0
Dario
Top achievements
Rank 1
answered on 23 Jun 2012, 10:19 AM
Thank you for the reply.
I solved it in a bid different but ugly way.
I used Request.Form["IDBooks"].ToString() in controller with which I got values of hidden fields (each hidden field contained one ID of a book). Than I parsed those IDs to int's and from there I could use them.

I will also probably try out your solution.
Tags
Grid
Asked by
Dario
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Dario
Top achievements
Rank 1
Share this question
or