Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4025694
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:52:42+00:00 2026-05-20T10:52:42+00:00

I use this AJAX to call my Excel Export action on the controller: $(#ExportToExcel).click(function

  • 0

I use this AJAX to call my Excel Export action on the controller:

$("#ExportToExcel").click(function () {
        // ajax call to do the export
        var urlString = "<%= System.Web.VirtualPathUtility.ToAbsolute("~/mvc/Indications.cfc/ExportToExcel")%>";
        var Jsondata =
                {
                    id: GetGUIDValue(),
                    viewName: "<%= VirtualPathUtility.ToAbsolute("~/Views/Indications/TermSheetViews/Swap/CashFlows.aspx")%>",
                    fileName: 'Cashflows.xls'
                }
        $.ajax({
            type: "POST",
            url: urlString,
            data: Jsondata,
            success: function (data) {

            }
        });
    });

Here is what the action looks like:

public ActionResult ExportToExcel(Guid? id, string viewName, string fileName)
        {
            IndicationBase indication = CachedTransactionManager<IndicationBase>.GetCachedTransactions(id.Value);
            return new ExcelResult<Chatham.Web.Models.Indications.ModelBase>
            (
                ControllerContext,
                viewName,
                fileName,
                indication.Model
            );
        }

Firebug runs through fine, no errors, and the code doesn’t error when running either, but nothing pops up on the screen. I am expecting to see a save dialog box to save the excel file.

What am I doing wrong?

EDIT: Here is my custom action,

public class ExcelResult<Model> : ActionResult
    {
        string _fileName;
        string _viewPath;
        Model _model;
        ControllerContext _context;

        public ExcelResult(ControllerContext context, string viewPath, string fileName, Model model)
        {
            this._context = context;
            this._fileName = fileName;
            this._viewPath = viewPath;
            this._model = model;
        }
         protected string RenderViewToString()
        {
            using (var writer = new StringWriter())
            {
                var view = new WebFormView(_viewPath);
                var vdd = new ViewDataDictionary<Model>(_model);
                var viewCxt = new ViewContext(_context, view, vdd, new TempDataDictionary(), writer);
                viewCxt.View.Render(viewCxt, writer);
                return writer.ToString();
            }
        }
        void WriteFile(string content)
        {
            HttpContext context = HttpContext.Current;
            context.Response.Clear();
            context.Response.AddHeader("content-disposition", "attachment;filename=" + _fileName);
            context.Response.Charset = "";
            context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            context.Response.ContentType = "application/ms-excel";
            context.Response.Write(content);
            context.Response.End();
        }

        public override void ExecuteResult(ControllerContext context)
        {
            string content = this.RenderViewToString();
            this.WriteFile(content);
        }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:52:43+00:00Added an answer on May 20, 2026 at 10:52 am

    You cannot call a file download on a ajax query because the browser will not trigger the file download popup.

    Instead of doing an ajax call to your controller method, simply use a

    windows.open("yoururl/ExportToExcel?id=yourid&etc...", null, null, null);
    

    And don”t forget to add your arguments.

    Hope this helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this ajax call to a doop.php . function doop(){ var old =
On my website I use a long polling jquery ajax function. This ajax call
I use the protocol jsonp to call web methods. I use this code for
I have a jquery Ajax call function that I use to submit form to
I use ajax call in my javascript: var myUrl = /cars/customer_cars $.getJSON( myUrl, {car_id:
I currently use this function to wrap executing commands and logging their execution, and
function populateGroups(){ var p =1; var groupNames = new Array(); $.ajax({ type: GET, url:
I use this call to create modal window using simplemodal ( http://www.ericmmartin.com/projects/simplemodal/ ): $.get(openform/,
I have an Ajax call that currently needs to be synchronous. However, while this
I have a simple $.ajax call that returns a response that looks like this:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.