4

SCRIPT

function PostExportValues(meter_id, range_type_id, start_date, end_date, returnUrl) {
    var meter = $("#meter_selection").val()[0];
    $.ajax({
        url: '@Url.Action("GridExportToExcel", "Widget")',
        type: 'POST',
        data: { MeterType: meter_id, DateRangeType: range_type_id, StartDate: start_date, EndDate: end_date, returnUrl: returnUrl, Meter: meter },
        success: function () {
            alert("Success.");
        },
        error: function () {
            alert("Error!");
        }
    });   //end ajax
} //end PostExportValues

CONTROLLER

public void GridExportToExcel(int MeterType, int DateRangeType, DateTime? StartDate, DateTime? EndDate, string returnUrl, int Meter)
{
    Customers customer = CustomerManager.GetCustomer(WebSecurity.CurrentUserId);
    //if start date is null, then set it to another early date.
    DateTime startDate = DateTimeManager.GetStartDate(StartDate, DateRangeType, customer.sno);
    //if end date is null, then set to date time now.
    DateTime endDate = DateTimeManager.GetEndDate(EndDate, StartDate);

    IQueryable<MeterReadingsForChart> meterReadings = MeterReadingManager.GetCustomerMeterReadings(customer.sno, MeterType, Meter, startDate, endDate, DateTimeManager.GetTimeIntervalTypeById(DateRangeType)).AsQueryable(); // MeterReadingManager.GetCustomerTotalMeterReadings(customer.sno, MeterType, startDate, endDate, DateTimeManager.GetTimeIntervalTypeById(DateRangeType)).AsQueryable();
    var table = MeterReadingManager.GetMeterReadingsPivot(meterReadings, MeterType);

    //table output some thing like following:
    //T1 T2 T3
    //10 20 25
    //13 23 21
    //15 26 27

    var grid = new GridView();
    grid.DataSource = table;
    grid.DataBind();

    Response.ClearContent();
    Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");

    Response.ContentType = "application/excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);

    grid.RenderControl(htw);

    Response.Write(sw.ToString());
    Response.End();

    //return View("Index");
}

Method GridExportToExcel is working and script alert message is Success. , but there is no action(nothing happens).

What am I missing? I expect that excel file automaticly download.

Thanks...

2
  • hmm without debugger its hard to tell, do you use firebug? if not download it and see what your post data sended Commented Jan 14, 2013 at 10:12
  • there is no error with sended data, its may be about requested data. Controller method is working (I debugged it). Also respose data is expected. I think, problem is the ajax call like @Steve say Commented Jan 14, 2013 at 10:24

3 Answers 3

9

You cant call a file download on a ajax query because the browser wont trigger the file download. dont use an ajax call to your controller method, you can use like

window.open("url/Exporttoexcel?id=");

with adding paramaters.

Sign up to request clarification or add additional context in comments.

Comments

0

The problem may be that you cant actually download via an ajax call. If you look at this: jQuery plugin for requesting ajax like file downloads.

This will help you!

Comments

0

Also you can use

window.location.href = "controllerName/GridExportToExcel?id="

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.