简体   繁体   English

从 javascript 方法将日期传递给 c# function

[英]pass date to c# function from javascript method

I am calling a c# function that returns a FileContentResult .我正在调用返回 FileContentResult 的FileContentResult However, the date is not being passed as a parameter to the c# function and always shows as null .但是,日期没有作为参数传递给c# function 并且始终显示为null what am i missing:我错过了什么:

Javascript code: Javascript 代码:

function exportResponses()
{  
     window.location = "/Blah/ExportResponse?
        questionnaireID=0&clinicID=0&responseStartDate='19/10/2019'";
}

C# function C# function

public FileContentResult ExportResponse(
     int questionnaireID = 0,
     int clinicID = 0, 
     DateTime? responseStartDate=null)
{

}

Try passing the date in the ISO8601 format (ie use Date().toISOString() before passing to the view).尝试以ISO8601格式传递日期(即在传递给视图之前使用Date().toISOString() )。

Try the following code:试试下面的代码:

function exportResponses()
{  
      var startDate = new Date('10/10/2019').toISOString();
      window.location = "/SMS/ExportResponse?
      questionnaireID=0&clinicID=0&responseStartDate="+startDate ;
}

And in your c# function try parsing parameter responseStartDate into DateTime in your required format.并在您的 c# function 尝试以您所需的格式将参数responseStartDate解析为DateTime

DateTime startDate = DateTime.ParseExact(responseStartDate, "yyyyMMdd")

Specific to your problem, you would have to send your date as a string to your Controller method:针对您的问题,您必须将日期作为字符串发送到您的Controller方法:

public FileContentResult ExportResponse(int questionnaireID = 0, int clinicID = 0, string responseStartDate=null)

And then you can process the string value accordingly in your method.然后您可以在您的方法中相应地处理字符串值。

try to separate the date to days, month and year, because is not good practice to use "/" in URL variables.尝试将日期分隔为天、月和年,因为在 URL 变量中使用“/”不是一个好习惯。


you try something like this:你尝试这样的事情:

window.location = "/SMS/ExportResponse?
      questionnaireID=0&clinicID=0&day="+startDate.day+"&month="+startDate.month+"&year="+startDate.year

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM