无法读取未定义的数据? JSON

时间:2015-06-30 06:02:52

标签: javascript jquery asp.net json kendo-mvvm

这是我使用js

保存值的代码
 self.Save = function (e) {

    if (vm_Currencies.selectedRow.CurrencyName == null) {
          alert("Fill Some Thing");
    }
    if (vm_Currencies.selectedRow.CurrencyCode != null) {
        debugger;

        var text1 = { "CurrencyCode": vm_Currencies.selectedRow.CurrencyCode, "CurrencyName": vm_Currencies.selectedRow.CurrencyName, "CurrencySign": vm_Currencies.selectedRow.CurrencySign, "DecimalPlaces": vm_Currencies.selectedRow.DecimalPlaces, "PositiveFormat": vm_Currencies.selectedRow.PositiveFormat, "NegativeFormat": vm_Currencies.selectedRow.NegativeFormat, "CurrencyStatus": vm_Currencies.selectedRow.CurrencyStatus };
        self.dsProduct.add(text1);
       ///////////error comes here while i call sync()
        self.dsProduct.sync();
     ////cannot read data of undefined
    }
    if (e.data.CurrencyName != null) {
        debugger;
        var text2 = { "CurrencyName": e.data.CurrencyName, "CurrencySign": e.data.CurrencySign, "DecimalPlaces": e.data.DecimalPlaces, "PositiveFormat": e.data.PositiveFormat, "NegativeFormat": e.data.NegativeFormat, "CurrencyStatus": e.data.CurrencyStatus };
        self.dsProduct.add(text2);
      /////////sucess
        self.dsProduct.sync();
      /////sucess
    }

}

text2有效,但是当我发送值top控制器以保存durring debuuging时,text1不起作用我检查格式两种格式是同样的,我应该怎么做同步值,以便服务器端接收它们进行更新

这是text1和text2

的控制台调试格式
   CurrencyCode: 20
   CurrencyName: "dollar"
   CurrencySign: "$"
   CurrencyStatus: "23"
   DecimalPlaces: 1
   NegativeFormat: "12"
   PositiveFormat: "12"
__proto__: Object

2 个答案:

答案 0 :(得分:1)

       self.Save = function (e) {
                    debugger;
                    if (vm_Currencies.selectedRow.CurrencyCode == null)
                    {
                        var GetValuesForSaving = { "CurrencyName": e.data.CurrencyName, "CurrencySign": e.data.CurrencySign, "DecimalPlaces": e.data.DecimalPlaces, "PositiveFormat": e.data.PositiveFormat, "NegativeFormat": e.data.NegativeFormat, "CurrencyStatus": e.data.CurrencyStatus };
                    self.dsProduct.add(GetValuesForSaving);
                    self.dsProduct.sync();
                    }

                    else
                    {
                        debugger;
                        var GetValuesForSaving = { "CurrencyC": vm_Currencies.selectedRow.CurrencyCode, "CurrencyName": vm_Currencies.selectedRow.CurrencyName, "CurrencySign": vm_Currencies.selectedRow.CurrencySign, "DecimalPlaces": vm_Currencies.selectedRow.DecimalPlaces, "PositiveFormat": vm_Currencies.selectedRow.PositiveFormat, "NegativeFormat": vm_Currencies.selectedRow.NegativeFormat, "CurrencyStatus": vm_Currencies.selectedRow.CurrencyStatus };
                        self.dsProduct.add(GetValuesForSaving);
                    self.dsProduct.sync();
                }

            }

答案 1 :(得分:0)

通过查看代码,我只想到的是vm_Currencies.selectedRow.CurrencyCode是"未定义"。

您可以使用浏览器调试器在运行时检查值。并尝试这个

if(vm_Currencies.selectedRow.CurrencyCode != "undefined")
{
//Code Here
}

另一个选项

if(vm_Currencies != "undefined")
{
//Code here
}