-1

I have POST function and i need to send from JS to Controller my values. String and integer values reaching without problem but double values is not reaching on server side but when i try on my local side it's working. Why it's acting like this?

Here my JS and Controller codes

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
            }
        });

Here is my C# Controller

    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!
    }

EDIT

Here is Request Payload from Google Chrome Network Tab

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

Note that my local PC and Server is in different countries.

saulyasar
  • 797
  • 1
  • 17
  • 45
  • Please use Chrome Developer Tools (Network tab) to show us the **exact payload** being posted. – mjwills Jul 17 '19 at 09:33
  • `var lon1 = $("#txtLon1").val();` Is that `lon1` a `string` perhaps? How would you convert it to a `double`? – mjwills Jul 17 '19 at 09:34
  • First thought would be: CORS policies – MrLine Jul 17 '19 at 09:34
  • Why aren't you using a model as a parameter? – adiga Jul 17 '19 at 09:36
  • 1
    Because it works on local machine and not on the server (in another country as you mentioned), it could be a culture issue as mentioned in this solution: https://stackoverflow.com/a/43680561/5746368 – jim1427 Jul 17 '19 at 09:37
  • @jim1427 i understand but my program is multilanguage so it can be bulgarian how can i make it dynamic – saulyasar Jul 17 '19 at 10:06
  • 1
    It is marked as a duplicate question since this question already has an answer here: https://stackoverflow.com/questions/32908503/c-sharp-mvc-controller-cannot-get-decimal-or-double-values-from-ajax-post-reques .If you had taken a look at this answer, you would have found your answer there. – Rahul Sharma Jul 17 '19 at 12:13
  • 1
    Saul if you put this code in your constructor or start of main it will adjust your program according to local culture . Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture; – Sezer Erdogan Jul 17 '19 at 12:16

1 Answers1

0

To avoid such cases, always generate your JSON in one string and send it to the Controller. In your case, you can do something like:

<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>

And you can get the values in your Controller like:

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");
}
Rahul Sharma
  • 7,768
  • 2
  • 28
  • 54