0

我正在尝试创建一个投票/投票应用程序,并希望跟踪为这些选项所做的投票数量。当进行投票时,会传入一个 id 并根据 id - 计数器加一。但是当我投票时,它会将另一个计数器设置回一个。

    // ID of the option selected:
    public int VotedID { get; set; }

    Counters for the options:
    public int BlueCornerPercent { get; set; }
    public int RedCornerPercent { get; set; }


    // Snippet of code - here is where I increase the counters. theFight is an instance of the model/entity.
    public void HandleVotes(Fight fight)
    {

        // Get full fight details:
        Fight theFight = db.Fights.Find(fight.FightId);

        // Get fighters id's in fight:
        var f1 = (from l in theFight.Fighters
                  select l).First();

        var f2 = (from l in theFight.Fighters
                  select l).Last();

        if (theFight.VotedID == f1.FighterID)
        {
            theFight.BlueCornerPercent++;
            db.SaveChanges();
        }

        else if (theFight.VotedID == f2.FighterID)
        {
            theFight.RedCornerPercent++;
            db.SaveChanges();
        }
    }

正如所见,我正在通过正在投票的“战斗”并从那里改变计数器......

4

2 回答 2

1

您可以尝试使用类似的东西:

    if (theFight.VotedID == f1.FighterID)
    {
        theFight.BlueCornerPercent++;
        db.Entry(theFight).Property(p => p.BlueCornerPercent).IsModified = true;
        db.Entry(theFight).Property(p => p.RedCornerPercent).IsModified = false;
        db.SaveChanges();
    }
于 2013-02-15T13:26:28.137 回答
0

我找到了解决方案,我需要在我的视图中包含计数器的隐藏属性,以防止它们重置。

于 2013-02-19T23:10:30.200 回答