[英]Sending a parameter from Javascript to a controller
I'm modifying an ASP.NET project that I didn't worked on before. 我正在修改一个以前没有处理过的ASP.NET项目。 I added a datepicker object :
我添加了一个datepicker对象:
<input id="datepicker" />
<script>
$("#datepicker").kendoDatePicker({
value: new Date()
change: UpdateVehiculesSurSite
});
</script>
And tried to modify the "UpdateVehiculesEnAttente" method to send the date picked to my controller : 并尝试修改“ 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;
}
The first problem is that the project run the javascript file first, so the datepicker isn't initialized. 第一个问题是该项目首先运行javascript文件,因此datepicker未初始化。 To try my Controller method, I gave "value" a date.
为了尝试我的Controller方法,我给了“值”一个日期。
My controller method is the following : 我的控制器方法如下:
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);
}
}
The result is that my Ajax request return error and launch the "montrerNotificationNoConnexion" method. 结果是我的Ajax请求返回错误并启动“ montrerNotificationNoConnexion”方法。
Any idea why ? 知道为什么吗?
EDIT : Using Firebug, I get this 编辑: 使用Firebug,我得到了
Do you think the problem could be that "date" contain "17%2F02%2F2016" instead of "17/02/2016" ? 您是否认为问题可能在于“日期”包含“ 17%2F02%2F2016”而不是“ 17/02/2016”?
EDIT2 : One of the problem was the string format. EDIT2:问题之一是字符串格式。 I changed it to "02/17/2016" but still not working.
我将其更改为“ 02/17/2016”,但仍无法正常工作。
You have to send the date in ISO 8601 format, then the MVC modelbinder can bind it. 您必须以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;
};
Usage: 用法:
data: {'date' : GlobalJsHelpers.Iso8601FromUtc(dateValueInUtc)}
You are defining your content type as contentType: 'application/json',
and then send data as simple text. 您将内容类型定义为
contentType: 'application/json',
然后将数据作为简单文本发送。
You should remove so that jquery uses the default 'application/x-www-form-urlencoded; charset=UTF-8'
您应该删除以便jquery使用默认的
'application/x-www-form-urlencoded; charset=UTF-8'
'application/x-www-form-urlencoded; charset=UTF-8'
type. 'application/x-www-form-urlencoded; charset=UTF-8'
类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.