将参数从 Javascript 发送到控制器
Sending a parameter from Javascript to a controller
我正在修改一个我以前没有参与过的 ASP.NET 项目。
我添加了一个日期选择器对象:
<input id="datepicker" />
<script>
$("#datepicker").kendoDatePicker({
value: new Date()
change: UpdateVehiculesSurSite
});
</script>
并尝试修改 "UpdateVehiculesEnAttente" 方法以将选择的日期发送到我的控制器:
function UpdateVehiculesEnAttente(){
var datepicker = $("#datepicker").data("kendoDatePicker");
console.log(typeof datepicker);
if (datepicker!==null && typeof datepicker !== "undefined"){
var value = datepicker.value();
console.log(value);
value = kendo.toString(value,"dd/MM/yyyy")
alert(value);
$(document).ready($.ajax({
url: 'Entrees_Sorties/Get_Vehicules_EnAttente',
type: 'POST',
data: {'date' : value},
contentType: 'application/json',
success: function (retour) {
$("#DivVehiculesEnAttente").html(retour);
console.log("update attente success");
},
error: function (xhr, status, error) {
montrerNotificationNoConnexion();
}
}));
//}
return false;
}
第一个问题是项目 运行 首先是 javascript 文件,所以日期选择器没有初始化。为了尝试我的 Controller 方法,我给了 "value" 一个日期。
我的控制器方法如下:
public ActionResult Get_Vehicules_EnAttente([DataSourceRequest] DataSourceRequest request, string date){
try{
List<List<Vehicule>> Data = new List<List<Vehicule>>();
DateTime dt = Convert.ToDateTime(date);
Data.Add(Models.Vehicule.Get_Vehicules_EnAttente_ByDate(dt, true));
return PartialView("VehiculesEnAttente", Data);
} catch (Exception e) {
WorkflowWCF.Log.enregistrer_erreur(new Exception("Une erreur est survenue lors de la récupération des véhicules planifiés", e));
return Json(new List<Vehicule>().ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
}
}
结果是我的Ajax请求return错误,启动"montrerNotificationNoConnexion"方法
知道为什么吗?
您认为问题可能出在 "date" 包含“17%2F02%2F2016”而不是“17/02/2016”吗?
EDIT2:问题之一是字符串格式。我将其更改为“02/17/2016”,但仍然无法正常工作。
您必须以 ISO 8601 格式发送日期,然后 MVC modelbinder 才能绑定它。
var GlobalJsHelpers = GlobalJsHelpers || {};
/// <summary>
/// Takes a utc js date and converts it to a iso8601 utc string
/// </summary>
GlobalJsHelpers.Iso8601FromUtc = function(utcDate) {
//create a two digit month string (01-12)
var month = ('0' + (utcDate.getUTCMonth() + 1)).substr(-2);
//create a two digit day string (01-31)
var day = ('0' + utcDate.getUTCDate()).substr(-2);
return utcDate.getUTCFullYear() + '-' + month + '-' + day;
};
用法:
data: {'date' : GlobalJsHelpers.Iso8601FromUtc(dateValueInUtc)}
您将内容类型定义为 contentType: 'application/json',
然后将数据作为简单文本发送。
您应该删除以便 jquery 使用默认的 'application/x-www-form-urlencoded; charset=UTF-8'
类型。
我正在修改一个我以前没有参与过的 ASP.NET 项目。 我添加了一个日期选择器对象:
<input id="datepicker" />
<script>
$("#datepicker").kendoDatePicker({
value: new Date()
change: UpdateVehiculesSurSite
});
</script>
并尝试修改 "UpdateVehiculesEnAttente" 方法以将选择的日期发送到我的控制器:
function UpdateVehiculesEnAttente(){
var datepicker = $("#datepicker").data("kendoDatePicker");
console.log(typeof datepicker);
if (datepicker!==null && typeof datepicker !== "undefined"){
var value = datepicker.value();
console.log(value);
value = kendo.toString(value,"dd/MM/yyyy")
alert(value);
$(document).ready($.ajax({
url: 'Entrees_Sorties/Get_Vehicules_EnAttente',
type: 'POST',
data: {'date' : value},
contentType: 'application/json',
success: function (retour) {
$("#DivVehiculesEnAttente").html(retour);
console.log("update attente success");
},
error: function (xhr, status, error) {
montrerNotificationNoConnexion();
}
}));
//}
return false;
}
第一个问题是项目 运行 首先是 javascript 文件,所以日期选择器没有初始化。为了尝试我的 Controller 方法,我给了 "value" 一个日期。
我的控制器方法如下:
public ActionResult Get_Vehicules_EnAttente([DataSourceRequest] DataSourceRequest request, string date){
try{
List<List<Vehicule>> Data = new List<List<Vehicule>>();
DateTime dt = Convert.ToDateTime(date);
Data.Add(Models.Vehicule.Get_Vehicules_EnAttente_ByDate(dt, true));
return PartialView("VehiculesEnAttente", Data);
} catch (Exception e) {
WorkflowWCF.Log.enregistrer_erreur(new Exception("Une erreur est survenue lors de la récupération des véhicules planifiés", e));
return Json(new List<Vehicule>().ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
}
}
结果是我的Ajax请求return错误,启动"montrerNotificationNoConnexion"方法
知道为什么吗?
您认为问题可能出在 "date" 包含“17%2F02%2F2016”而不是“17/02/2016”吗?
EDIT2:问题之一是字符串格式。我将其更改为“02/17/2016”,但仍然无法正常工作。
您必须以 ISO 8601 格式发送日期,然后 MVC modelbinder 才能绑定它。
var GlobalJsHelpers = GlobalJsHelpers || {};
/// <summary>
/// Takes a utc js date and converts it to a iso8601 utc string
/// </summary>
GlobalJsHelpers.Iso8601FromUtc = function(utcDate) {
//create a two digit month string (01-12)
var month = ('0' + (utcDate.getUTCMonth() + 1)).substr(-2);
//create a two digit day string (01-31)
var day = ('0' + utcDate.getUTCDate()).substr(-2);
return utcDate.getUTCFullYear() + '-' + month + '-' + day;
};
用法:
data: {'date' : GlobalJsHelpers.Iso8601FromUtc(dateValueInUtc)}
您将内容类型定义为 contentType: 'application/json',
然后将数据作为简单文本发送。
您应该删除以便 jquery 使用默认的 'application/x-www-form-urlencoded; charset=UTF-8'
类型。