2015-10-30 22 views
0

GetTeams後端成功地返回一個列表..但我甚至不能console.log數據也不做任何事情。如果我有GetTeams回報:嘗試從後端接收POST數據時發生內部服務器錯誤; ASP.NET MVC與AngularJS

return Json("testing", JsonRequestBehavior.AllowGet); 

它工作正常。我無法理解這裏發生了什麼,爲什麼這不起作用。有人能幫我嗎?由於

ASP.NET MVC

[HttpPost] 
    public JsonResult GetTeams(int leagueId) 
    { 
     try 
     { 
      using (var sdb = new SoccerDataEntities()) 
      { 
       var teamList = (from teams in sdb.footballTeams 
           orderby teams.name ascending 
           where teams.league_id == leagueId 
           select teams).ToList(); 

       return Json(teamList, JsonRequestBehavior.AllowGet); 
      } 


     } 
     catch (Exception e) 
     { 
      return Json(false, JsonRequestBehavior.DenyGet); 
     } 
    } 

角控制器

HeadlinesFactory.getTeamsFromLeague(league.LeagueId) 
     .success(function (data) {     
      console.log(data); 
     }); 

編輯:

public class footballTeam 
{ 
    public footballTeam(); 

    public string coach { get; set; } 
    public virtual footballLeague footballLeague { get; set; } 
    public int id { get; set; } 
    public int league_id { get; set; } 
    public string name { get; set; } 
    public int season_id { get; set; } 
    public int team_id { get; set; } 
    public string TeamDetails { get; set; } 
} 

public class footballLeague 
{ 
    public footballLeague(); 

    public virtual ICollection<footballFeedUpdate> footballFeedUpdates { get; set; } 
    public virtual ICollection<footballPlayer> footballPlayers { get; set; } 
    public virtual ICollection<footballPromotionRelegation> footballPromotionRelegations { get; set; } 
    public virtual ICollection<footballTeam> footballTeams { get; set; } 
    public bool? groups { get; set; } 
    public int league_id { get; set; } 
    public string name { get; set; } 
} 

SQL列

  • ID
  • TEAM_ID
  • league_id
  • season_id
  • 教練
  • TeamDetails
+0

就像一個測試,你可以刪除使用語句。這類事情幾乎總是與處理可查詢事件有關。儘管如此,你正在用.ToList()解決,如果情況並非如此,你會認爲你至少會得到一個錯誤。 –

+1

作爲一個額外的測試,您是否也可以在您的select中返回一個teams.name集合,以查看它是否是無法序列化的團隊的屬性。 –

+0

@JoshuaBelden好吧現在就讓我試試 – user1189352

回答

2

使用投影只返回你所需要的數據,並同時保證您不會從查詢中返回任何Linq類型。

 var teamList = (from teams in sdb.footballTeams 
      orderby teams.name ascending 
      where teams.league_id == leagueId 
      select new 
      { 
      id = teams.id, 
      team_id = teams.team_id, 
      league_id = teams.league_id, 
      season_id = teams.season_id, 
      name = teams.name, 
      coach = teams.coach, 
      TeamDetails = teams.TeamDetails 
      }).ToList(); 

     return Json(teamList, JsonRequestBehavior.AllowGet); 
+0

工作! TY! – user1189352

+0

歡迎您!很高興我能幫上忙。 –

相關問題