[英]pass date to c# function from javascript method
我正在調用返回 FileContentResult 的FileContentResult
。 但是,日期沒有作為參數傳遞給c#
function 並且始終顯示為null
。 我錯過了什么:
Javascript 代碼:
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)
{
}
嘗試以ISO8601格式傳遞日期(即在傳遞給視圖之前使用Date().toISOString()
)。
試試下面的代碼:
function exportResponses()
{
var startDate = new Date('10/10/2019').toISOString();
window.location = "/SMS/ExportResponse?
questionnaireID=0&clinicID=0&responseStartDate="+startDate ;
}
並在您的 c# function 嘗試以您所需的格式將參數responseStartDate
解析為DateTime
。
DateTime startDate = DateTime.ParseExact(responseStartDate, "yyyyMMdd")
針對您的問題,您必須將日期作為字符串發送到您的Controller
方法:
public FileContentResult ExportResponse(int questionnaireID = 0, int clinicID = 0, string responseStartDate=null)
然后您可以在您的方法中相應地處理字符串值。
嘗試將日期分隔為天、月和年,因為在 URL 變量中使用“/”不是一個好習慣。
你嘗試這樣的事情:
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.