SelectedValue在一个例程中保持= 1,而另一个类似的例程工作

时间:2016-05-17 20:27:38

标签: c# asp.net database gridview entity-framework-6

好的,我绝对难过。这就是我的意思: 这是ManageTeams.aspx.cs BindTeams()例程,它完美运行:

public void BindTeams()
{
    using (var db = new SoccerDbContext())
    {
        int lId = 0;

        IQueryable<Team> teams = db.Teams;

        //if any league is selected, then filter below.  Do not run if first run.
        //if first run, the whole list is displayed.  This also happens on form reset.
        if (0 < Convert.ToInt32(drp_Leagues1.SelectedValue) && !firstRun) 
        {
            var leagueId = Convert.ToInt32(drp_Leagues1.SelectedValue);
            teams = teams.Where(t => t.CurrentLeagueId == leagueId);
            lId = leagueId;
        }
        lbl_Debug.Text = "1Sindex=" + drp_Leagues1.SelectedIndex.ToString() +
                       ", 2Sindex=" + drp_Leagues2.SelectedIndex.ToString() +
                       ", 1Svalue=" + drp_Leagues1.SelectedValue +
                       ", 2Svalue=" + drp_Leagues2.SelectedValue +
                       ", leagueId=" + lId +
                       ", firstRun=" + firstRun;
        grd_Teams.DataSource = teams.ToList();
        grd_Teams.DataBind();
    }
}

关联的Team.cs类:

public class Team
    {
        public int TeamId { get; set; }

        [Required]
        [MaxLength(100)]
        public string TeamName { get; set; }

        public DateTime DateEstablished { get; set; }

        //Navigation property -> Each team has many players
        public List<Player> Players { get; set; }

        //Navigation property -> Each team belongs to a specific league
        public League CurrentLeague { get; set; }

        //Foreignkey -> The associated LeagueId
        //[Required]
        [ForeignKey("CurrentLeague")]
        public int CurrentLeagueId { get; set; }

    }

这是ManagePlayers.aspx.cs BindPlayers_byTeam()例程,工作:

public void BindPlayers_byTeam()
{
    using (var db = new SoccerDbContext())
    {
        int tId = 0;
        IQueryable<Player> players = db.Players;

        //if any team is selected, then filter below.  Do not run if first run.
        //if first run, the whole list is displayed.  This also happens on form reset.
        if (0 < Convert.ToInt32(drp_Teams1.SelectedValue) && !firstRun)
        {
            var teamId = Convert.ToInt32(drp_Teams1.SelectedValue);
            players = players.Where(t => t.CurrentTeamId == teamId);
            tId = teamId;
        }
        lbl_Debug.Text = "Sindex=" + drp_Teams1.SelectedIndex.ToString() +
                         ", Svalue=" + drp_Teams1.SelectedValue +
                         ", teamId=" + tId +
                         ", firstRun=" + firstRun;
        grd_Players.DataSource = players.ToList();
        grd_Players.DataBind();
    }
}

关联的Player.cs类(正确编写):

public class Player
   {
       public int PlayerId { get; set; }

       [Required]
       [MaxLength(100)]
       public string FullName { get; set; }

       public DateTime BirthDate { get; set; }

       public DateTime LicenseDate { get; set; }

       public int TeamCount { get; set; }

       [ForeignKey("CurrentTeam")]
       public int CurrentTeamId { get; set; }

       public Team CurrentTeam { get; set; }
   }

两个Bind例程的全部目的是分别返回具有相同CurrentLeagueId / CurrentTeamId的团队/玩家的过滤列表。在两个例程之间,除了变量名之外没有功能差异,但后一个例程中的SelectedValue值始终返回值1,此时它应返回与下拉列表中所选团队一致的值。无论我在那里选择什么,我总是得到1的价值!因此得到的结果是:

Concatenated screenshots of two tables

前一个例程没有这个问题,并且在功能上等同于后一个例程。我已经到处寻找代码中的任何差异来解释异常,在属性框和标记中。我没有看到什么是错的。它可能是什么?

同样,这句话:

if (0 < Convert.ToInt32(drp_Leagues1.SelectedValue) && !firstRun) 
        {
            var leagueId = Convert.ToInt32(drp_Leagues1.SelectedValue);
            teams = teams.Where(t => t.CurrentLeagueId == leagueId);
            lId = leagueId;
        }

...在功能上与其他例程相同:

if (0 < Convert.ToInt32(drp_Teams1.SelectedValue) && !firstRun)
        {
            var teamId = Convert.ToInt32(drp_Teams1.SelectedValue);
            players = players.Where(t => t.CurrentTeamId == teamId);
            tId = teamId;
        }

......然而另一个却没有。

0 个答案:

没有答案