Pass parameter to the controller function

Hi, I have this action in my controller :

[HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "Administrator")]
        public ActionResult excuteGrid(string sql, [FromBody] DataManagerRequest dm)
        {
            try
            {
                if (!string.IsNullOrEmpty(sql))
                {
                    var conn = _db.Database.GetDbConnection();
                    if (conn.State == System.Data.ConnectionState.Closed)
                    {
                        conn.Open();
                    }

                    var cmd = conn.CreateCommand();
                    cmd.CommandText = sql;

                    if (sql.ToLower().StartsWith("select"))
                    {
                        var reader = cmd.ExecuteReader();
                        List list = new List();
                        while (reader.Read())
                        {
                            var obj = new ExpandoObject() as IDictionary;
                            for (int i = 0; i < reader.FieldCount; i++)
                            {
                                var col = reader.GetName(i);
                                var value = reader.GetValue(i);
                                obj[col] = value;
                            }
                            list.Add(obj as dynamic);
                        }

                        IEnumerable dataSource = list.AsEnumerable();

                        var operation = new DataOperations();
                        if (dm.Search != null)
                            dataSource = operation.PerformSearching(dataSource, dm.Search);

                        var count = dataSource.Cast().ToList().Count;

                        if (dm.Skip != 0)
                        {
                            dataSource = operation.PerformSkip(dataSource, dm.Skip);   //Paging
                        }
                        if (dm.Take != 0)
                        {
                            dataSource = operation.PerformTake(dataSource, dm.Take);
                        }


                        return dm.RequiresCounts ? Json(new { result = dataSource.Cast(), count = count }) : Json(dataSource.Cast());
                    }

                    int result = cmd.ExecuteNonQuery();
                    if (result > 0)
                    {
                        return Json(new { message = $"Row Affected : {result }" });
                    }
                }
                return Json(new { message = "SQl Statement Not Found" });
            }
            catch (Exception ex)
            {

                return Json(new { message = ex.Message });
            }
        }


I want to pass sql as parameter from view to display the result on Grid and here is my code in view :

           

               
                                adaptor="UrlAdaptor"
                                crossDomain="true">
               

               
                                     pageSize="20">
               

           
 
               
I do not know exact column names and istead construct ExpandoObject at runtime, adding columns as needed.
If I pass list of ExpandoObject to the Grid component, it wont work - shows empty grid.

How canI pass parameter to the controller function from Grid and get data later to display it in Grid?

Regards,
Zohir

2 Replies

ZA zohir alhammoud March 5, 2021 10:44 AM UTC

 here is my code in view :

<section>
            <ejs-grid id="Grid2" allowPaging="true" load="onLoad">

                <e-data-manager url="/ExecuteSql/executeGrid?sql=sql"
                                adaptor="UrlAdaptor"
                                crossDomain="true">
                </e-data-manager>

                <e-grid-pagesettings pageCount="5"
                                     pageSize="20">
                </e-grid-pagesettings>

            </ejs-grid>
        </section>


MS Manivel Sellamuthu Syncfusion Team March 5, 2021 10:51 AM UTC

Hi Zohir, 
 
Greetings from Syncfusion support. 
 
Query: If I pass list of ExpandoObject to the Grid component, it wont work - shows empty grid. 
 
Could you please explain more about this query with video demonstration or screenshot, with network tab request. 
 
Query: How can pass parameter to the controller function from Grid and get data later to display it in Grid? 
 
To add a custom parameter to the data request, you can use the addParams method of Query class. For this we need to assign the Query object with additional parameters to the grid query property. 
Please refer the documentation link for more information. 
 
 
Regards, 
Manivel 


Loader.
Up arrow icon