2017-06-14 72 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