Hi,
Is there a way to pass values from my view to my grid export function?
I have a custom search at the top of my view that generates my grid via stored procedure with parameters and I need to be able to export the search results that are currently in my grid.
Here is my code...
View:
<div class="container">
<div class="row">
<div class="col-xs-2" id="p1"><input type="text" class="form-control input-sm" id="txtState" placeholder="State" ></div>
<div class="col-xs-2 col-half-offset" id="p2"><input type="text" class="form-control input-sm" id="txtCampaign" placeholder="Campaign" ></div>
<div class="col-xs-2 col-half-offset" id="p3">@Html.EJ().DatePicker("ScanDateFrom").WatermarkText("Scan Date From").Value(ViewBag.datevalue) </div>
<div class="col-xs-2 col-half-offset" id="p4">@Html.EJ().DatePicker("ScanDateTo").WatermarkText("Scan Date To").Value(ViewBag.datevalue)</div>
<div class="col-xs-2 col-half-offset" id="p5">@Html.EJ().Button("buttonnormal").Text("Search").Width("75px").ClientSideEvents(e => e.Click("click"))</div>
</div>
</div>
<div id="divStateGrid" style="visibility:hidden">
@(Html.EJ().Grid<object>("ResponseRatesByState")
.ToolbarSettings(toolBar => toolBar.ShowToolbar().ToolbarItems(items =>
{
items.AddTool(ToolBarItems.ExcelExport);
}))
.Mappers(map => map.ExportToExcelAction("ExportResponseStateGrid"))
.Datasource(ds => ds.URL(@Url.Action("ResponseRateByState"))
.Adaptor(AdaptorType.UrlAdaptor))
.AllowPaging()
.AllowSorting()
.AllowFiltering()
.FilterSettings(filter => { filter.FilterType(FilterType.Excel); })
.EnableAltRow()
.Columns(col =>
{
col.Field("Campaign").HeaderText("Campaign").Add();
col.Field("State").HeaderText("State").Add();
col.Field("MailDate").HeaderText("Mail Date").Format("{0:M/d/yyyy}").Add();
col.Field("LeadCount").HeaderText("Lead Count").Add();
col.Field("OrderCount").HeaderText("Order Count").Add();
col.Field("ResponseRate").HeaderText("Response Rate").Format("{0:P2}").Add();
})
.ClientSideEvents(eve => { eve.RowSelected("rowSelected"); })
.ClientSideEvents(eve => eve.ActionBegin("beginstate"))
)
</div>
Controller:
public ActionResult ResponseRateByState(Syncfusion.JavaScript.DataManager dm,string state,string campaign, string scandatefrom, string scandateto)
{
DataResult result = new DataResult();
DataTable dtResult = new DataTable();
int count = 0;
dtResult = GetResponseRateByStateGridData("spGetResponseRateByState",state,campaign, scandatefrom,scandateto);
IEnumerable Data = (from DataRow row in dtResult.Rows
select new DKDatabaseModelClass.ResponseRatesByState
{
Campaign = row["Campaign"].ToString(),
State = row["State"].ToString(),
MailDate = Convert.ToDateTime(row["MailDate"]),
LeadCount = (int)row["LeadCount"],
OrderCount = (int)row["OrderCount"],
ResponseRate = (decimal)row["ResponseRate"]
}).ToList();
Syncfusion.JavaScript.DataSources.DataOperations operation = new Syncfusion.JavaScript.DataSources.DataOperations();
// List<string> aggregateFields = new List<string>();
if (dm.Search != null)
Data = operation.PerformSearching(Data, dm.Search);
if (dm.Where != null)
Data = operation.PerformWhereFilter(Data, dm.Where, dm.Where[0].Condition); //perform filtering
//if (dm.Aggregates != null)
//{
// for (var i = 0; i < dm.Aggregates.Count; i++)
// aggregateFields.Add(dm.Aggregates[i].Field);
// result.aggregate = operation.PerformSelect(Data, aggregateFields);
//} //perform aggregation.
//result.count = Data.AsQueryable().Count();
if (dm.Skip != null && dm.Skip != 0)//skiped while rendering checkbox
Data = operation.PerformSkip(Data, dm.Skip);
if (dm.Take != null && dm.Take != 0)//skiped while rendering checkbox
Data = operation.PerformTake(Data, dm.Take);
result.result = Data;//passed the whole dataSource directly for checkbox rendering
return Json(result, JsonRequestBehavior.AllowGet);
// return Json(new { result = Data, JsonRequemystBehavior.AllowGet);
}
Here is what my usual export functions look like....I need to be able to pass the search parameters in as well(I need to replace the ??? with values from my view:
public void ExportResponseStateGrid(string GridModel)
{
ExcelExport exp = new ExcelExport();
var DataSource = GetResponseRateByStateGridData("spGetResponseRateByState", ???, ???, ???, ??? );
IEnumerable Data = (from DataRow row in DataSource.Rows
select new DKDatabaseModelClass.ResponseRatesByState
{
Campaign = row["Campaign"].ToString(),
State = row["State"].ToString(),
MailDate = Convert.ToDateTime(row["MailDate"]),
LeadCount = (int)row["LeadCount"],
OrderCount = (int)row["OrderCount"],
ResponseRate = (decimal)row["ResponseRate"]
}).ToList();
GridProperties obj = ConvertGridObject(GridModel);
exp.Export(obj, Data, "ResponseRatesByStateMailDate.xlsx", ExcelVersion.Excel2010, false, false, "flat-saffron");
}