2013-01-13 51 views
0

我使用MVC C#我有的getJSON檢查空數據

public class Person 
{ 
     public int TypeId { get; set; } 
     public string Code { get; set; } 

} 

一個對象我有retruns JSON對象這個對象是這樣

return Json(personCodeStore.GetPersonCodes(id).Select(x=>new {text =x.Code, value =x.TypeId}), JsonRequestBehavior.AllowGet); 

如何添加一個代碼指標到我的JSON返回數據,這樣我可以檢查空數據

$.getJSON('../PersonList?id=1', function (data) { 
if (data==????) 
{ 
alert("has data"); 
} 
else 
{ 
alert("is Empty"); 
} 
} 

回答

0

你能只檢查你知道財物的人對象會有?這樣的事情:

if (data.Id) { 
    // We got back something with an Id property... probably a Person 
} else { 
    // What else could we get back?!? 
} 
+0

我得到undefined –

+0

那麼你希望得到什麼數據?您可以檢查哪些數據屬性? – jakerella

+0

我不得不做 if(data [0] .Id){ //我們拿回了一些帶有Id屬性的東西......可能是一個人 } else { //我們還能回來什麼?!? } –