在Asp.net mvc中以csv格式获取数据

时间:2015-06-07 16:37:54

标签: jquery asp.net-mvc csv

尝试以csv格式获取数据,无法使用jquery获取此数据,但是当我使用硬编码值传递参数时,这同样有效,例如

  

它的工作..

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

但是当我尝试使用jquery传递参数值时它不起作用,可能是我的jquery调用中有一些问题无法弄明白。 这是我的

  

无效的jquery代码

 @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) {

            });
        });

方法

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();
        }

我的jquery有什么问题,请帮忙..

0 个答案:

没有答案