user1189352
user1189352

Reputation: 3885

Internal Server Error when trying to receive POST data from backend; ASP.NET MVC with AngularJS

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

Upvotes: 0

Views: 261

Answers (1)

Joshua Belden
Joshua Belden

Reputation: 10503

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

Upvotes: 2

Related Questions