使用Jquery传递一个DateTime值到Action方法



我试图通过一个Datetime值使用jquery控制器,但它显示我的错误

parameters字典包含一个空的parameter条目非空类型的FromDate。DateTime' for方法"System.Web.Mvc.ActionResult报告(系统。字符串,系统。字符串,系统。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);
        }

如有任何帮助,不胜感激

试试这个:

$('#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) {
      });
});

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

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

相关内容

  • 没有找到相关文章

最新更新