This example demonstrates how to get the key values of all GridView selected rows and pass them to a controller.
Set up the GridView, specify its CallbackRouteValues property, and handle the SelectionChanged event. In the event handler, call the GetSelectedFieldValues method to obtain the specified field values.
// Index.cshtml
@Html.Partial("GridViewEditingPartial", Model)
// GridViewEditingPartial.cshtml
@Html.DevExpress().GridView(settings => {
settings.Name = "grid";
settings.KeyFieldName = "PersonID";
settings.CallbackRouteValues = new { Controller = "Home", Action = "GridViewEditingPartial" };
settings.CommandColumn.Visible = true;
settings.CommandColumn.ShowSelectCheckbox = true;
settings.ClientSideEvents.SelectionChanged = "OnSelectionChanged";
...
}).Bind(Model).GetHtml()
var selectedIDs;
function OnSelectionChanged(s, e) {
s.GetSelectedFieldValues("PersonID", GetSelectedFieldValuesCallback);
}
function GetSelectedFieldValuesCallback(values) {
// Obtain all selected keys
selectedIDs = values.join(',');
}
Use one of the following approaches to pass the values to the controller:
-
Handle the grid's client-side MVCxClientGridView.BeginCallback event. In the handler, assign selected values to the MVCxClientBeginCallbackEventArgs.customArgs argument property to pass them to the server as a request parameter. Handle the grid's CallbackRouteValues.Action method to get all selected field values from the
e.customArgs
property.// GridViewEditingPartial.cshtml @Html.DevExpress().GridView(settings => { ... settings.ClientSideEvents.BeginCallback = "OnBeginCallback"; ... // Index.cshtml function OnBeginCallback(s, e) { e.customArgs["selectedIDs"] = selectedIDs; }
// HomeController.cs public ActionResult GridViewEditingPartial() { string _selectedIDs = Request.Params["selectedIDs"]; ViewData["_selectedIDs"] = _selectedIDs; return PartialView(list.GetPersons()); }
-
In a button's
Click
event, write all selected values to a hidden input element, and get these values from any controller's post-action.// Index.cshtml @using(Html.BeginForm("Index", "Home")) { @Html.Hidden("selectedIDsHF"); @Html.DevExpress().Button(settings => { settings.Name = "btnSubmit"; settings.Text = "Submit Form"; settings.UseSubmitBehavior = true; settings.ClientSideEvents.Click = "OnSubmitClick"; }).GetHtml() }
// HomeController.cs public ActionResult Index(string selectedIDsHF) { string _selectedIDs = selectedIDsHF; return View(list.GetPersons()); }