How to not to download empty excelsheet and show alert no records found
hi all iam working with downloading excellsheet in mvc but its downloading the issue is it is downloading for empty data also below is my code how to not to download empty data and show alert can any body help mein controller i have written like this
public void ExportClientsListToCSV()
{
StringWriter sw = new StringWriter();
sw.WriteLine("\"User Name\",\"In Time\",\"Out Time\",\"Client Info\",\"User Role\",\"Activity\"");
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment;filename=User_Track.csv");
Response.ContentType = "text/csv";
var abc = TempData["test"];
IEnumerable<PAN_LOGINTRK> test = abc as IEnumerable<PAN_LOGINTRK>;
//object test = (abc);
foreach (var line in test)
{
var role = "";
if (line.userrole == 1)
{
role = "CSAdmin";
}
else if (line.userrole == 2)
{
role = "Buyer";
}
else if (line.userrole == 3)
{
role = "Supervisor";
}
else if (line.userrole == 4)
{
role = "OrgAdmin";
}
sw.WriteLine(string.Format("\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\",\"{5}\"",
line.username,
line.Intime,
line.Outtime,
"Client IP: "+line.ClientIP+" Client XIP: "+line.ClientXIP+" Client Agent: "+line.ClinetAgent,
role,
line.Activity));
}
Response.Write(sw.ToString());
Response.End();
}
in view i have written like
<div class="col-md-4" style="color: Black;padding-top:10px;padding-bottom:10px;">
<input type="button" id="btnDwnldExp" value="Export" onclick="Export()" class="btn btn-default1 pull-right btn-success">
<input type="button" id="btnDwnldSrch" value="@Resources.Resource.btnSearch" onclick="getSearchData()" class="btn btn-default1 pull-right btn-success">
</div>
function Export() {
var ExportAction = '@Url.RouteUrl("Default", new RouteValueDictionary(new { action = "ExportClientsListToCSV", controller = "UserTrack" }), "http", ConfigurationManager.AppSettings["ContextPath"])'
location.href = ExportAction;
}