I want to get rows which selected on Devexpress MVC GridView at a time. Not at each item clicked.
Reference: How to get all GridView selected keys and pass them to a Controller
You can collect all selected values (for example, keys) on the client side via the ASPxClientGridView.GetSelectedFieldValues method and pass them to:
- The GridView callback action via the e.customArgs dictionary (Passing Values to Controller Action Through Callbacks);
- Any controller post action via a hidden input element.
Example code snippet:
<script type="text/javascript">
var selectedIDs;
function OnBeginCallback(s, e) {
//Pass all selected keys to GridView callback action
e.customArgs["selectedIDs"] = selectedIDs;
}
function OnSelectionChanged(s, e) {
s.GetSelectedFieldValues("PersonID", GetSelectedFieldValuesCallback);
}
function GetSelectedFieldValuesCallback(values) {
//Capture all selected keys
selectedIDs = "";
for (var index = 0; index < values.length; index++) {
selectedIDs += values[index] + ",";
}
if (selectedIDs.length > 0)
selectedIDs = selectedIDs.substring(0, selectedIDs.length - 1);
}
function OnClick(s, e) {
//Show all selected keys on client side
alert(selectedIDs);
}
function OnSubmitClick(s, e) {
//Write all selected keys to hidden input. Pass them on post action.
$("#selectedIDsHF").val(selectedIDs);
}
</script>
Controller
namespace Sample.Controllers {
public class HomeController : Controller {
PersonsList list = new PersonsList();
[HttpGet]
public ActionResult Index() {
return View(list.GetPersons());
}
[HttpPost]
public ActionResult Index(string selectedIDsHF) {
//Get all selected keys from hidden input
string _selectedIDs = selectedIDsHF;
return View(list.GetPersons());
}
public ActionResult GridViewEditingPartial() {
//Get all selected keys from e.customArgs on GridView callback
string _selectedIDs = Request.Params["selectedIDs"];
ViewData["_selectedIDs"] = _selectedIDs;
return PartialView(list.GetPersons());
}
}
}
Note: The client-side GetSelectedKeysOnPage Method returns key values of selected rows displayed within the current page.
References:
GridView - How to get selected rows as an action parameter
GridView - How to get values of selected rows in the Controller's Action
How do I get my selected GridView rows into a Javascript variable?
DevExpress MVC GridView - How to get cell click event
How to highlight a particular row in a page of a DevExpress MVC GridView?