Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Nate M 18 posts 148 karma points
    Aug 15, 2024 @ 18:21
    Nate M
    0

    How to call ActionResult method from View

    Hey everyone, I'm trying to call an ActionResult from my View so that I can export data to excel using EPPlus. So far I've been able to get a list of all records in the database to display on the page.

    Next I want to use a button to call the ExportToExcel function inside my surfacecontroller, but I get this error:

    OutputStream is not available when a custom TextWriter is used.
    

    Here is how I'm trying to call the function:

    <a href="@{Html.RenderAction("ExportToExcel","TsunamiSurveyResponseSurface");}" class="btn btn-success">
       <i class="far fa-share-square"></i> Export to Excel
    </a>
    

    Here is my surfacecontroller function:

    public ActionResult ExportToExcel()
        {
            List<Response> data = db.Response.OrderBy(x => x.SurveyDate).ToList();    //FETCH DATA FROM DATABASE
    
            //CONVERT DATA TO A FORMAT SUITABLE FOR EXCEL
            List<object[]> excelData = new List<object[]>
            {
    
                //HEADER ROW
                new object[] { "Survey Date","City","State","Zipcode","Phone","Email","Organization","Profession","Advanced Warning Notice","Available Notice Devices","Received Notice Devices","Notice Received Time","Comments" }
            };
    
            foreach(Response d in data)
            {
                excelData.Add(new object[] { d.SurveyDate.ToString(),d.City,d.State,d.Zipcode,d.Phone,d.Email,d.Organization,d.Profession,d.AdvancedWarningNotice,d.AvailableNoticeDevices,d.ReceivedNoticeDevices,d.NoticeReceivedTime,d.Comment });
            }
    
            //CREATE EXCEL PACKAGE
            using(ExcelPackage p = new ExcelPackage())
            {
                ExcelWorksheet worksheet = p.Workbook.Worksheets.Add("Sheet1");
    
                //LOAD DATA TO EXCEL WORKSHEET
                for(int x = 0;x < excelData.Count;x++)
                {
                    for(int y = 0;y < excelData[x].Length;y++)
                    {
                        ExcelRange cell = worksheet.Cells[x + 1,y + 1];
                        cell.Value = excelData[x][y];
    
                        if(excelData[x][y] is DateTime)
                        {
                            cell.Style.Numberformat.Format = "yyyy-mm-dd HH:mm";
                        }
                    }
                }
    
                //CONVERT PACKAGE TO BYTE ARRAY
                byte[] fileBytes = p.GetAsByteArray();
    
                //RETURN EXCEL FILE
                return File(fileBytes,"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet","SurveyData.xlsx");
            }
        }
    

    Now the error is being thrown as soon as the page loads.

    Can anyone tell me what I'm doing wrong?


    Resloved:

    I changed this:

    <a href="@{Html.RenderAction("ExportToExcel","TsunamiSurveyResponseSurface");}" class="btn btn-success">
       <i class="far fa-share-square"></i> Export to Excel
    </a>
    

    to this:

    <a href="@Url.Action("ExportToExcel","TsunamiSurveyResponseSurface")" class="btn btn-success">
       <i class="far fa-share-square"></i> Export to Excel
    </a>
    

    and everything works as expected now.

Please Sign in or register to post replies

Write your reply to:

Draft