[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 :