2017-06-14 108 views
1

我是Jquery的新手。我有一個jquery的一段代碼,它分配PAN名稱來標記從j#中轉換後從c#字典返回的。當我獲得警戒值(粗線)時。它顯示未定義。jquery - 從C#詞典中獲取價值

以下是我的代碼。

    success: function (data) { 
 
         var msgi = data.MSG; 
 
         var panno; 
 
         if (msgi != "A" && msgi != "B") { 
 
          alert(msgi); ////// Here undefined is showing 
 
          $("#Item3_PANNumber").focus(); 
 
          $("#Item3_PANNumber").val(''); 
 
         }       
 

 
        }
/////// This is last part of method. 
 

 
          if (words.Length > 0) 
 
          { 
 
           PANCard pc = new PANCard(); 
 

 
           if (words[0] == "1" && words[2] == "E") 
 
           { 
 
            pc.PANNumber = words[1]; 
 
            pc.LastName = words[3]; 
 
            pc.FirstName = words[4]; 
 
            pc.MiddleName = words[5]; 
 
            pc.Title = words[6]; 
 
            pc.LastUpdated = words[7]; ////DateTime.ParseExact(words[7], @"d/M/yyyy", culture); 
 

 
            var panno = pc.Title + " " + pc.FirstName + " " + pc.MiddleName + " " + pc.LastName; 
 
            Returndict.Add("MSG", "B"); 
 
            return Json(panno, JsonRequestBehavior.AllowGet); 
 
           } 
 
          }

您的幫助可能會讚賞。

感謝 Nandkumar S.

+0

請您做一個減少代碼的例子嗎? (請參閱:http://www.sscce.org/)我相當肯定你發佈的大部分代碼與問題並不相關。也可以考慮在調試器中加入C#代碼和JS代碼,以查看您要返回到網頁的什麼值,以及發生錯誤的代碼行處發生了什麼。 – millimoose

+0

親愛的millimoose, thianks您的答覆。我縮短了代碼。 – satputenandkumar

+0

你從來沒有真正從你的C#代碼或'pc'返回'ReturnDict',只有'panno'字符串。除非將其放入所示代碼之外的JSON響應中。 – millimoose

回答

0

使用類響應:

1. Create a response class 
 

 
public class JsonReaponse 
 
{ 
 
    public bool Success { get; set; } 
 
    public string Message { get; set; } 
 
    public object Data { get; set; } = new List<string>(); 
 
} 
 

 
2. Use a response class with object 
 

 
JsonReaponse reaponse = new JsonReaponse(); 
 
if (words.Length > 0) 
 
{ 
 
    PANCard pc = new PANCard(); 
 
    if (words[0] == "1" && words[2] == "E") 
 
    { 
 
     pc.PANNumber = words[1]; 
 
     pc.LastName = words[3]; 
 
     pc.FirstName = words[4]; 
 
     pc.MiddleName = words[5]; 
 
     pc.Title = words[6]; 
 
     pc.LastUpdated = words[7]; ////DateTime.ParseExact(words[7], @"d/M/yyyy", culture); 
 

 
     var panno = pc.Title + " " + pc.FirstName + " " + pc.MiddleName + " " + pc.LastName; 
 
     reaponse.Message = "Your Message"; 
 
     reaponse.Data = panno; 
 
     return Json(reaponse, JsonRequestBehavior.AllowGet); 
 
    } 
 
} 
 

 
      
 
3. In jQuery 
 

 
success: function (res) { 
 
    var msgi = res.Message; 
 
    var panno = res.Data; 
 
    if (msgi != "A" && msgi != "B") { 
 
     alert(msgi); ////// Here undefined is showing 
 
     $("#Item3_PANNumber").focus(); 
 
     $("#Item3_PANNumber").val(''); 
 
    }       
 
}

+0

感謝Govind, 這幫了我很多...... :) – satputenandkumar