0

the GetTeams backend successfully is returning a list.. but I can't even console.log the data nor do anything. If i have GetTeams return:

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

it works fine though. I can't understand what's going on here and why this doesn't work. Can someone help me? Thanks

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);
        }
    }

Angular Controller

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

EDIT:

Classes

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 Columns

  • id
  • team_id
  • league_id
  • season_id
  • name
  • coach
  • TeamDetails
user1189352
  • 3,628
  • 12
  • 50
  • 90
  • Just as a test, could you remove the using statement. This kind of thing is almost always having to do with the queryable being disposed. Albeit, you're resolving with a .ToList() and if that wasn't the case, you'd think you'd at least get an error. – Joshua Belden Oct 30 '15 at 16:58
  • 1
    As an additional test, could you also just return a collection of teams.name in your select to see if it's a property on teams that's not able to be serialized. – Joshua Belden Oct 30 '15 at 17:01
  • @JoshuaBelden okay let me try right now – user1189352 Oct 30 '15 at 17:06
  • @JoshuaBelden ah, when I change to "select teams.name" it works, but I do need the full object.. how can I go about doing that? – user1189352 Oct 30 '15 at 17:11
  • 2
    side note, `.success` is depreciated, and you should use `.then` instead, wherever possible – Claies Oct 30 '15 at 17:11
  • @Claies ah ty i will look into that – user1189352 Oct 30 '15 at 17:12
  • back to your original issue, it sounds like there is some property in the `teams` class that can't be serialized properly; can you show what the class definition looks like? – Claies Oct 30 '15 at 17:14
  • You need to share the [full details for the Internal Server Error](http://stackoverflow.com/questions/5385714/deploying-website-500-internal-server-error). This will help you narrow down what exactly is going wrong. – Jasen Oct 30 '15 at 17:17
  • @Claies updated the OP with the classes and also the columns in the SQL table – user1189352 Oct 30 '15 at 17:18
  • For what it's worth, I have had nothing but problems and frustration from trying to serialize and return data entities diurectly. I'd highly recommend recommend you look at serializing viewmodels or anonymous objects to prevent the serialization logic from traversing your entire entities and all their associated references, collections and potentially lazy loaded properties. – Nick Albrecht Oct 30 '15 at 17:18
  • this looks like you have `teams` which refer to `footballLeague` which in turn refers back to `teams`. I suspect the serializer is having a hard time with the circular reference here. Do you have an initializer in your app to deal with circular references? – Claies Oct 30 '15 at 17:21
  • @Jasen hm tried the suggestions from the link you posted but i don't see it giving me more details.. i'll spend more time to look more into that later in the work day. – user1189352 Oct 30 '15 at 17:25
  • @NickAlbrecht fml me too man............ ty for the suggestions to look into – user1189352 Oct 30 '15 at 17:25
  • 1
    take a look at this article: http://johnnycode.com/2012/04/10/serializing-circular-references-with-json-net-and-entity-framework/ – Claies Oct 30 '15 at 17:25
  • @Claies ah maybe that's it, i'm going to try disregarding that property (thankfully i dont' need that one specifically) and see what happens.. – user1189352 Oct 30 '15 at 17:26
  • 1
    I still recommend giving that linked article a read, even though you did find a solution to the current issue. That article discusses a couple changes that can enforce a global policy for serialization which may help deal with many issues, not just this specific one. – Claies Oct 30 '15 at 17:49
  • @Claies i definitely will. i have it bookmarked! i just have a deadline right now.. so can't spend too much time on it at this very moment.. i love understanding WHY though and not just a quick solution, so i very much appreciate the knowledge. ty again! – user1189352 Oct 30 '15 at 17:55
  • 1
    The accepted answer is using a projection into an anonymous object so that works. – Nick Albrecht Oct 30 '15 at 20:12

1 Answers1

2

Use a projection to return only the data you need and to also ensure you're not returning any Linq types from the query.

        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);
Joshua Belden
  • 10,273
  • 8
  • 40
  • 56