簡體   English   中英

RelaodGrid操作時jqGrid PostData中的DateTime

[英]DateTime in jqGrid PostData while RelaodGrid Operation

請考慮ASP.Net MVC5項目中JavaScript文件中的以下虛擬代碼。

var $grid = $("#grid")
var postData = $grid.jqGrid("getGridParam", "postData");
postData["xyzId"] = $("#xyzId").val();
postData["date"] = $("#date").val();
$grid.setGridParam({ postData: postData });
$grid.setGridParam({ datatype: "json", page: 1 }).trigger("reloadGrid");

以下是Controller的ActionMethod請求到達的地方......

public ActionResult GetXyz(int xyzId, DateTime? date)

保持日期可以為空是需要的,因為它是可選的選擇。

問題: .Net Framework的文化設置中的日期格式是“DD / MM / YYYY”,當我們發送“02/10/2016”時它接受為“2016年2月10日”(這是錯誤的btw)但是當我們發送時“2016年10月16日”它將其視為空。 這只發生在我們在jqGrid Reload操作中傳遞日期時。 然而,在正常的ajax呼叫中,Everythins運行良好,例如“02/10/2016”=>“2016年10月2日”和“2016年10月16日”=>“2016年10月16日”。

有人可以幫忙嗎?

建議僅使用與語言環境無關的格式來傳輸數據。 這是數字,日期,...的通用規則$("#date").val() 分配給postData.xyzId 之前 ,您可以將$("#date").val()轉換為YYYY-MM-DD格式(日期的ISO 8601格式)? 我認為ISO日期格式是MVC中使用的Newtonsoft.Json默認格式。

通常使用formatter: "date" ,帶有newformat屬性的可選formatoptions 以內部方式保存並僅傳輸與區域設置無關的數據,但用戶將看到本地特定日期(基於您在colModel明確指定的formatoptions.newformat或從grid.locale-XX.js繼承的隱式值)。

將日期捕獲為字符串

public ActionResult GetXyz(int xyzId, string date="")

然后以傳統方式格式化:

var Date= DateTime.ParseExact(date, "dd/MM/yyyy", CultureInfo.InvariantCulture);

希望這會有所幫助:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM