简体   繁体   中英

pass date to c# function from javascript method

I am calling a c# function that returns a FileContentResult . However, the date is not being passed as a parameter to the c# function and always shows as null . what am i missing:

Javascript code:

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

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

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.

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

Specific to your problem, you would have to send your date as a string to your Controller method:

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.


you try something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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