I need to collect the selected rows and submit them to the server for processing.
Is there a code example that I missed somewhere?
I need to submit to a MVC5 controller.
Thanks!
I probably need to provide more information. I am using the URL Adapter approach like:
@Html.EJS().Grid("UrlAdaptor").DataSource(ds => ds.Url(@Url.Action("UrlDatasource", "Grid")).Adaptor("UrlAdaptor")).AllowSorting().Columns(col => ....
|
@{
List<object> toolbarItems = new List<object>();
toolbarItems.Add(new { text = "Selected", tooltipText = "Selected", id = "Selected" });
}
@Html.EJS().Grid("Grid").DataSource(dataManager => { dataManager.Url("/Home/UrlDatasource").Adaptor("UrlAdaptor"); }).Columns(col =>
{
col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
col.Field("CustomerID").HeaderText("Customer Name").Width("150").Add();
col.Field("Freight").HeaderText("Freight").Width("120").Format("C2").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
col.Field("ShipName").HeaderText("Ship Name").Width("150").Add();
col.Field("ShipCountry").HeaderText("Ship Country").Width("150").Add();
}).AllowPaging().Toolbar(toolbarItems).ToolbarClick("toolbarClick").Render()
<script>
function toolbarClick(args) {
if (args.item.id === 'Selected') {
var selectedRecord = this.getSelectedRecords(); //selected records
var ajax = new ej.base.Ajax({
url: "/Home/SelectedRecords",
type: "POST",
contentType: "application/json",
data: JSON.stringify(selectedRecord),
successHandler: function (response) {
console.log(JSON.parse(response));
},
failure: function (response) {
alert(response);
}
});
ajax.send();
}
}
</script>
|
|
Controller page
|
Hi Praveenkumar, thanks for your help on this.
Your solution almost works for me. It is my ignorance with the MVC world that is the problem.
I need the posted data to redirect to a different view.
I am not sure that can be done with and ajax call.
Please advise.
Thanks!
Also, note that the post controller is dynamically generating a view based on the selected records, so the the idea of putting a redirect in the ajax success method does not work.
Thanks!
Hi Praveenkumar, I have figured a way to do what I need but it is not elegant. I am wondering if there is a better more elegant way that allows for the default model binder to work.
I am using code like this:
<!-- In the View -->
<form action="/fred/barny" id="barnyForm" method="post">
@Html.AntiForgeryToken()
@Html.EJS().Button("primarybtn").Content("barny").Click("submit").CssClass("e-primary").Render()
</form>
<script>
barnyForm.onsubmit = function (e) {
var grid = document.getElementById("grid").ej2_instances[0];
var selectedrecords = grid.getSelectedRecords(); // get the selected records.
$("<input />").attr("type", "hidden")
.attr("name", "rowData")
.attr("value", JSON.stringify(selectedrecords))
.appendTo("#barnyForm");
return true;
}
</script>
//In the controller
[HttpPost]
public ActionResult Barny(FormCollection data)
{
var rows = data["rowData"];
dynamic d = JsonConvert.DeserializeObject(rows);
//do the logic to generate the data for the view here
....
return View();
}
Am I missing some better more elegant way of handling this?
Thanks!