使用Jquery将DateTime值传递给Action方法

时间:2015-06-04 11:52:27

标签: jquery asp.net-mvc datetime

我正在尝试使用jquery将Datetime值传递给控制器​​,但它显示错误

  

参数字典包含参数的空条目   ' FROM日期'非可空类型的System.DateTime'方法   ' System.Web.Mvc.ActionResult Report(System.String,System.String,   System.DateTime,System.DateTime,Int32,Int32,Int32)'   但在调试时我得到的价值。我正在为此生成报告但无法对此进行排序。请帮助解决问题。

查看

<a href="@Url.Action("Report", new { id = "PDF" })" class="btn-primary" id="exportbutton"> Export as PDF&nbsp;<i class="glyphicon glyphicon-print"></i></a>&nbsp;


@Html.TextBoxFor(m => m.FromDate, new { @readonly = true, @class = "date-picker form-control" }) 
@Html.TextBoxFor(m => m.ToDate, new { @readonly = true, @class = "date-picker form-control" }) 
     $('#exportbutton').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();
            var id = "PDF";
            $.post(
                '@Url.Action("Report", "Reports")',
                { id: id, SelectedAccountType: accountType, FromDate: fromDate, ToDate: toDate, SelectedAccount: accountId, SelectedUser: userId, SelectedTeam: teamId },
                 function (data) {
                 });
        });

控制器

public ActionResult Report(string id, string SelectedAccountType, DateTime FromDate, DateTime ToDate, int SelectedAccount, int SelectedTeam, int SelectedUser)
        {
            LocalReport lr = new LocalReport();
            string path = Path.Combine(Server.MapPath("~/Report"), "ReportList1.rdlc");
            if (System.IO.File.Exists(path))
            {
                lr.ReportPath = path;
            }
            else
            {
                return View("Index");
            }
            var OrderInfoList = reportService.GetReportsList(SelectedAccountType, FromDate, ToDate, SelectedAccount, SelectedTeam, SelectedUser);
            ReportDataSource rd = new ReportDataSource("MyDataSet", OrderInfoList);
            lr.DataSources.Add(rd);
            string reportType = id;
            string mimeType;
            string encoding;
            string fileNameExtension;
            string deviceInfo = "<DeviceInfo>" +
            "  <OutputFormat>" + id + "</OutputFormat>" +
            "  <PageWidth>8.5in</PageWidth>" +
            "  <PageHeight>11in</PageHeight>" +
            "  <MarginTop>0.5in</MarginTop>" +
            "  <MarginLeft>0.5in</MarginLeft>" +
            "  <MarginRight>0.5in</MarginRight>" +
            "  <MarginBottom>0.5in</MarginBottom>" +
            "</DeviceInfo>";

            Warning[] warnings;
            string[] streams;
            byte[] renderedBytes;

            renderedBytes = lr.Render(
                reportType,
                deviceInfo,
                out mimeType,
                out encoding,
                out fileNameExtension,
                out streams,
                out warnings);
            return File(renderedBytes, mimeType);
        }

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:1)

试试这个:

$('#exportbutton').click(function () {
      var accountType = $('#SelectedAccountType').val(); 
      var fromDate = new Date($('#FromDate').datepicker('getDate'));//Get it from datepicker, this will get the selected date.
      var toDate = new Date($('#ToDate').datepicker('getDate'));
      var accountId = $('#SelectedAccount').val();
      var userId = $('#SelectedUser').val(); 
      var teamId = $('#SelectedTeam').val();
      var id = "PDF";
      $.post(
           '@Url.Action("Report", "Reports")',
            { id: id, SelectedAccountType: accountType, FromDate: fromDate, ToDate: toDate, SelectedAccount: accountId, SelectedUser: userId, SelectedTeam: teamId },
            function (data) {
      });
});

答案 1 :(得分:0)

我建议您将日期作为字符串传递,然后将它们转换为日期。

 public ActionResult Report(string id, string SelectedAccountType, string FromDate, string ToDate, int SelectedAccount, int SelectedTeam, int SelectedUser)
   {
     to_date = Convert.ToDateTime(ToDate);
       ////
   }