Click here to Skip to main content
14,735,555 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Team

I have a ActionResult method and its using [HttpGet] and another with [Httpost], now the challenge is to find a way to call the method that will allow my download functionality to work on View. Meaning i have a button, but the button functionality from the View is to download the record of my table. How do i achieve this using this below attempt logic? I have tried to use separate View to access that method, i was not successful

What I have tried:

<pre>  [HttpGet]
        public ActionResult SearchPeopleDetails()
        {

            RegCoursesViewModel regCoursesView = new RegCoursesViewModel();
            return View(cb.RegPeopleLists.ToList());
        }


        [HttpPost]
        public ActionResult SearchPeopleDetails(RegCoursesViewModel eNtsaRegistration_2)
        {
            if(ModelState.IsValid)
            {
                eNtsaRegistration_2.regPeopleLists.LoginID = Int32.Parse("SearchValue");
                cb.RegPeopleLists.Add(eNtsaRegistration_2.regPeopleLists);
                cb.SaveChanges();
                return RedirectToAction("Download_DelegateReportData");
            }
            return RedirectToAction("Download_DelegateReportData");
        }

[HttpGet]
      public ActionResult Download_DelegateReport()
  {
      RegCoursesViewModel regCoursesView = new RegCoursesViewModel();

      return View(regCoursesView);
  }

//Downloading the Delegate-Information as Excel-Report.

        public void Download_DelegateReportData()
        {
            var _cb = new eNtsaRegistration_2();

            var data = (from q in _cb.RegPeopleLists
                        select new { 
                        LoginID = q.LoginID,
                        Name = q.Name,
                        SSID = q.SISID,
                        Role = q.Role,
                        LastActivity = q.LastActivity,
                        TotalActivity = q.TotalActivity

                        }).ToList();
            var b = ConvertToDataTable(data);
            string attachment = "attachment; filename=eNtsaTraining-Registration.xls";
            Response.ClearContent();
            Response.AddHeader("content-disposition", attachment);
            Response.ContentType = "application/vnd.ms-excel";
            string tb = "";

            foreach (DataColumn dc in b.Columns)
            {
                Response.Write(tb + dc.ColumnName);
                tb = "\t";
            }
            Response.Write("\n");
            int i;
            foreach (DataRow dr in b.Rows)
            {
                tb = "";
                for (i = 0; i < b.Columns.Count; i++)
                {
                    Response.Write(b + dr[i].ToString());
                    tb = "\t";
                }
                Response.Write("\n");
            }
            Response.End();
        }
Posted
Updated 22-Sep-20 5:15am

1 solution

Download_DelegateReportData is not an action, so you cannot redirect to it.

You should also avoid using Response.Write inside an action method. Use an appropriate action result instead.

And you're not sending an Excel file; you're sending a tab-separated values file. You need to use the correct MIME type.
[HttpGet]
public ActionResult Download_DelegateReportData()
{
    using (var cb = new eNtsaRegistration_2())
    {
        var data = (from ... ).ToList();
        var b = ConvertToDataTable(data);
        
        var ms = new MemoryStream();
        using (var writer = new StreamWriter(ms))
        {
            writer.WriteLine(string.Join("\t", b.Columns.Cast<DataColumn>().Select(c => c.ColumnName)));
            foreach (DataRow dr in b.Rows)
            {
                writer.WriteLine(string.Join("\t", dr.ItemArray));
            }
        }
        
        ms.Seek(0L, SeekOrigin.Begin);
        return new FileStreamResult(ms, "text/tab-separated-values") 
        {
            FileDownloadName = "eNtsaTraining-Registration.tsv"
        };
    }
}
   
Comments
gcogco10 23-Sep-20 2:37am
   
Thanks Richard, now how will i access this method from my button on View? will i use redirect? the button must be able to download from the method itself excel format.
Richard Deeming 23-Sep-20 3:49am
   
The same way you'd call any other action. If your button submits a form, it can either point directly to the download action, or the action it submits to can redirect you to the download action. If you use an <a> element instead, you can set its href to point directly to the download action.
gcogco10 23-Sep-20 3:51am
   
Mine should download, this is my front end button in bootstrap.



Download Excel

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900