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.
<script type="text/javascript">
$(function () {
$('#btn1').click(function () {
simpleGrid.PerformCallback();
});
});
function OnBeginCallback(s, e) {
var selectedValues = s.GetSelectedKeysOnPage();
e.customArgs["Id"] = "";
for (var i = 0; i < selectedValues.length; i++) {
e.customArgs["Id"] += selectedValues[i] + ',';
}
}
</script>
@Html.Partial("ProductsPartial", Model.Data)
<div id="btn1">
btn
</div>
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:
@Html.DevExpress().GridView(
settings =>
{
settings.Name = "simpleGrid";
settings.KeyFieldName = "Id";
settings.CallbackRouteValues = new { Controller = "yourController", Action = "Postback" };
settings.SettingsText.Title = "simpleGridWithPostback";
settings.Settings.ShowTitlePanel = true;
settings.Settings.ShowStatusBar = GridViewStatusBarMode.Visible;
settings.SettingsPager.Mode = GridViewPagerMode.ShowAllRecords;
settings.SettingsPager.AllButton.Text = "All";
settings.SettingsPager.NextPageButton.Text = "Next >";
settings.SettingsPager.PrevPageButton.Text = "< Prev";
settings.Width = new System.Web.UI.WebControls.Unit(200);
settings.Columns.Add("Id");
settings.Columns.Add("Name");
settings.CommandColumn.Visible = true;
settings.CommandColumn.ShowSelectCheckbox = true;
settings.ClientSideEvents.BeginCallback = "OnBeginCallback";
}).Bind(Model).GetHtml()
And finally the controller in which you can process the data
public ActionResult Postback()
{
String data = Request["Id"];
}
This way you can process all the data you want server side