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..