I am currently trying to make a preloader in partial view in ASP MVC 4.
I have a main view and a partial view. I am currently loading data inside the webgrid of the partial view. There is a button inside the partial view "Export Data" which is a submit button. After clicking it, it will go to the controller where the codes for exporting webgrid to excel will run. It takes too long to load when the data is big, so I need to show a "Please wait.. the data is being extracted.." or a spinning loader to show that the page is loading.
Here is my code
Controller
public void GetExcel()
{
var db = Database.Open("DBConnection");
var data = db.Query(TempData["export"].ToString());
ViewBag.Query = data;
WebGrid wd = new WebGrid(ViewBag.Query, canPage: false, rowsPerPage: 100, canSort: false);
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=" + TempData["reportName"] + ".xls");
Response.ContentType = "application/octet-stream";
Response.Write(wd.GetHtml(tableStyle: "border: 1px solid #a59f9f;"));
Response.End();
}
This is for the partial view
if (Model.results == null){
@Html.ActionLink("Export Data", "GetExcel", null, new { @class="btn btn-danger btn-sm", @disabled="disabled" })
}
else
{
@Html.ActionLink("Export Data", "GetExcel", null, new { @class="btn btn-danger btn-sm" })
}
<div class="spacerQuery"></div>
var grid = new WebGrid(Model.results, canPage: true, rowsPerPage: 20, canSort: true, ajaxUpdateContainerId: "grid2", ajaxUpdateCallback: "callBack");
@grid.GetHtml(
htmlAttributes: new { id = "grid2" }
, tableStyle: "table table-bordered table-condensed small"
, headerStyle:"",
rowStyle: "val",
alternatingRowStyle: "total",
fillEmptyRows: true
)
This is the code for the main view
<div id="grid">
@Html.Partial("_result", Model.results)
</div>
I've tried using json to catch the buttonclick, then call the function in controller, then just use .addClass("hidden")
and .removeClass("hidden")
, but the codes for exporting excel won't work.