I use ASP.NET C# MVC3 with razor templates and I want to know the following:
I have a DevExpress GridView
in which I can select rows.
When selected, I want to pass them into a Javascript variable. How do I do this?
And what if I have selected multiple rows at the same time, can I get them in an array or something?
Edit: Thank you for the comment, I now have the following 'JQuery' function:
$(function () { // Verwijder functie
$('#select').click(function () {
var Delete = confirm("Weet u zeker dat u de geselecteerde records wilt verwijderen?");
if (Delete) {
//Verwijder Funtie
var test = ;
alert(test);
} else {
//Niks
}
});
});
I need to get the Id of the selected rows into the variable 'test', I tried it with GetSelectedFieldValuesCallback and GetSelectedFieldValues. But this is not working as I'm expecting. If someone can provide some example I would really appreciate that.
I have found the solution. The following function provides the data I want:
For people who had/have the same problem.
Change "Index" in
to whatever you named your gridview, and change "Id" to the column you want to get.
You can also get multiple data from a single row, to do this you need to add another for-loop in the
function OnGetSelectedFieldValues(result)
as following:You wil also need to change the getter as following
Index.GetSelectedFieldValues("Id;YOUROTHERCOLUMN", OnGetSelectedFieldValues);
I hope this will help in the future for other users.
There is a surprising lack of examples for one of the most basic operations of a grid. Especially if you want to send rows back to the server for further processing.
The problem with grid.GetSelectedFieldValues() is that it generates a postback. Meaning you would need a second postback to actually send data back to the server.
The most elegant solution I was able to achieve is to use grid.GetSelectedKeysOnPage(). This will return selected key fields defined through settings.KeyFieldName = "Id";
the view which will display your grid.
It is important that you create your grid in a separate partial view (don't know why, but that is what it says on the devexpress page.
The "ProductsPartial" partial view:
And finally the controller in which you can process the data
This way you can process all the data you want server side