我具有POST功能,我需要将JS的值从JS发送到Controller。字符串和整数值没有问题,但在服务器端没有达到double值,但是当我在本地尝试时,它正在工作。为什么会这样呢?
这是我的JS和控制器代码
var cmbvdf = $('#cmbvolDec').data('kendoComboBox');
var cmbvdfval = cmbvdf.value();
var cmbadf = $('#cmbamtDec').data('kendoComboBox');
var cmbadfval = cmbadf.value();
var cmbCur = $('#cmbCurrency').data('kendoComboBox');
var cmbcurrency = cmbCur.value();
var cmtheme = $('#cbmTheme').data('kendoComboBox');
var cmbvaltheme = cmtheme.value();
var cmblang = $('#cmbLanguage').data('kendoComboBox');
var cmbvallang = cmblang.value();
var custname = $("#txtCustName").val();
var websitetitle = $("#txtWebName").val();
var zoom = $("#txtZoom").val();
var lat1 = $("#txtLat1").val();
var lon1 = $("#txtLon1").val();
var url = '../Setting/SaveRecords';
$.ajax({
type: "POST",
url: url,
data:JSON.stringify({ 'volumeDecimalFactor': cmbvdfval, 'amountDecimalFactor': cmbadfval, 'currency': cmbcurrency, 'theme': cmbvaltheme, 'language': cmbvallang, 'customerName': custname, 'lat1': lat1, 'lon1': lon1, 'web': websitetitle, 'zoom': zoom }),
contentType: 'application/json, charset=utf-8',
traditional: true,
success: function (data) {
//do something
}
});
这是我的C#控制器
public ActionResult SaveRecords(string volumeDecimalFactor, string amountDecimalFactor, string currency, string theme, string language, string customerName, double? lat1, double? lon1, string web, double? zoom)
{
//when i'm on my local machine i can get double values without problem.
//when i published on server i cannot get values and return to me null!
}
编辑
这是从Google Chrome网络标签页请求有效载荷
amountDecimalFactor: "2"
currency: "4"
customerName: "Fuel Card System"
language: "1"
lat1: "41.071789"
lon1: "28.980325"
theme: "4"
volumeDecimalFactor: "2"
web: "WebProject"
zoom: "5.2
请注意,我的本地PC和服务器在不同的国家/地区。
答案 0 :(得分:0)
为避免这种情况,请始终在一个字符串中生成JSON
并将其发送到Controller
。就您而言,您可以执行以下操作:
<script type="text/javascript">
var cmbvdf = $('#cmbvolDec').data('kendoComboBox');
var cmbvdfval = cmbvdf.value();
var cmbadf = $('#cmbamtDec').data('kendoComboBox');
var cmbadfval = cmbadf.value();
var cmbCur = $('#cmbCurrency').data('kendoComboBox');
var cmbcurrency = cmbCur.value();
var cmtheme = $('#cbmTheme').data('kendoComboBox');
var cmbvaltheme = cmtheme.value();
var cmblang = $('#cmbLanguage').data('kendoComboBox');
var cmbvallang = cmblang.value();
var custname = $("#txtCustName").val();
var websitetitle = $("#txtWebName").val();
var zoom = $("#txtZoom").val();
var lat1 = $("#txtLat1").val();
var lon1 = $("#txtLon1").val();
var json = {
cmbvdfval : cmbvdfval,
cmbadfval : cmbadfval,
cmbcurrency : cmbcurrency ,
cmbvaltheme : cmbvaltheme ,
cmbvallang : cmbvallang ,
custname : custname ,
websitetitle : websitetitle ,
zoom : zoom ,
lat1 : lat1 ,
lon1 : lon1
};
$.ajax({
url: '@Url.Action("SaveRecords", "Setting")',
type: 'post',
dataType: "json",
data: { "json": JSON.stringify(json)},
success: function (result) {
//do something
},
error: function (error) {
console.log(error)
}
});
</script>
您可以像这样获得Controller
中的值:
using System.Web.Script.Serialization;
[HttpPost]
public ActionResult SaveRecords(string json)
{
var serializer = new JavaScriptSerializer();
dynamic jsondata = serializer.Deserialize(json, typeof(object));
//Get your variables here from AJAX call
var zoom = jsondata["zoom"];
var lat1 =jsondata["lat1"];
//Do something with your variables here
return Json("Success");
}