0

trying to get data in csv format, having trouble to get this using jquery, but the same is working when i pass the parameter using hardcoded value, like

its working..

<a href="@Url.Action("Export", new {SelectedAccountType = "1", FromDate = "2014-02-02", ToDate = "2014-02-02", SelectedAccount = "", SelectedUser = "", SelectedTeam = "" })" class="btn-primary" id="exportbutton2"> Export as CSV</i></a>

but when i try to pass the parameter value using jquery its not working,may be there is some problem in my jquery call can not able to figure it out. here is my

jquery code that is not working

 @Html.TextBox("FromDate", null, new { @readonly = true, @class = "date-picker form-control" })
@Html.TextBoxFor(m => m.ToDate, new { @readonly = true, @class = "date-picker form-control" })
...
// other controls..
<a  class="btn-primary" id="exportbutton2"> Export as CSV</a>
$('#exportbutton2').click(function () {
            var accountType = $('#SelectedAccountType').val();
            var fromDate = $('#FromDate').val();
            var toDate = $('#ToDate').val();
            var accountId = $('#SelectedAccount').val();
            var userId = $('#SelectedUser').val();
            var teamId = $('#SelectedTeam').val();
            $.post(
            '@Url.Action("Export","Reports")',
            { "SelectedAccountType": accountType, FromDate: fromDate, ToDate: toDate, SelectedAccount: accountId, SelectedUser: userId, SelectedTeam: teamId },
            function (data) {

            });
        });

method

public void Export(string SelectedAccountType, string FromDate, string ToDate, string SelectedAccount, string SelectedTeam, string SelectedUser)
        {
            var to_date = Convert.ToDateTime(ToDate);
            var from = Convert.ToDateTime(ToDate);
            var ac = Convert.ToInt32(SelectedAccount);
            var te = Convert.ToInt32(SelectedTeam);
            var us = Convert.ToInt32(SelectedUser);
            StringWriter sw = new StringWriter();
            var list = reportService.GetReportsList(SelectedAccountType, from, to_date, ac, te, us);
            //First line for column names
            sw.WriteLine("\"Borrower First Name\",\"Borrower Last Name\",\"Requested URL\"");

            foreach (ReportList item in list)
            {
                sw.WriteLine(string.Format("\"{0}\",\"{1}\",\"{2}\"",
                                           item.BorrowerFirstName,
                                           item.BorrowerLastName,
                                           item.RequestedURL));
            }

            Response.AddHeader("Content-Disposition", "attachment; filename=test.csv");
            Response.ContentType = "text/csv";
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
            Response.Write(sw);
            Response.End();
        }

whats wrong with my jquery , please help..

Teerth
  • 169
  • 3
  • 5
  • 19
  • 1
    Why are you making all your parameters `string`? They should be `DateTime` and `int` (and remove all the `Convert.ToDateTime()` and `Convert.ToInt32()` methods). There is also no need to quote the names - just use `{ SelectedAccountType: accountType, FromDate: fromDate, etc. }`. And what is `var id = "PDF";`? - you don't appear to use the value of `id` anywhere. Also it should be `$.get(...)`. What is not working? Are the method parameters not populated in the controller method? –  Jun 08 '15 at 01:42
  • i have made all changes as your suggestion , method parameters are populated with the exact value, but still its not calling that method. and also there is no need of var id = "PDF"; – Teerth Jun 08 '15 at 09:33
  • 1
    The anchor is a `GET` request, but you're using jQuery as a `POST` request. Unless this action is decorated with `HttpPost`, you may want to change that to use `$.get` – Brad Christie Jun 08 '15 at 12:51
  • @ Brad Christie i have changed it to $.get( '@Url.Action("Export", "Reports")', { SelectedAccountType: accountType, FromDate: fromDate, ToDate: toDate, SelectedAccount: accountId, SelectedUser: userId, SelectedTeam: teamId }, function (data) { }); but still the same problem any idea ? – Teerth Jun 08 '15 at 13:08
  • @StephenMuecke i have made all changes as your suggestion , method parameters are populated with the exact value, but still its not calling that method. and also there is no need of var id = "PDF"; – Teerth Jun 08 '15 at 13:11
  • Perhaps the [answers here](http://stackoverflow.com/questions/16086162/handle-file-download-from-ajax-post) might help –  Jun 08 '15 at 13:17

0 Answers0