我正在尝试创建一个投票/投票应用程序,并希望跟踪为这些选项所做的投票数量。当进行投票时,会传入一个 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();
}
}
正如所见,我正在通过正在投票的“战斗”并从那里改变计数器......